Discussion

Sign in or Join to comment or subscribe

[deleted]

2021-08-01T08:13:27Z ago

let fee = 123_00;       // $123 (12300 cents, apparently)
let fee = 12_300;       // $12,300 (woah, that fee!)
let amount = 12345_00;  // 12,345 (1234500 cents, apparently)
let amount = 123_4500;  // 123.45 (4-fixed financial)
let amount = 1_234_500; // 1,234,500

sure will never confuse anyone why number is 100 time smaller, once b/c someone misplaced an optional underscore

like, why? if you want decimals – there is a literal point

Jerod Santo

Jerod Santo

Omaha, Nebraska

Jerod co-hosts The Changelog, crashes JS Party, and takes out the trash (his old code) once in awhile.

2021-08-02T13:30:09Z ago

The (100% optional) underscores represent where you’d put a comma, not a decimal. They do not change the number value whatsoever, just like commas don’t.

123_00 === 12_300
// true

The reason you’d want to do this is because it’s easier to grok at a glance what a large number is:

let total = 1234500114
// vs
let total = 1_234_500_114

If you showed me the first total and asked me what order of magnitude it was, I’d have to count how many digits there are. If you showed me the second total and asked me the same question, I would know it’s 1 billion almost immediately.

Player art
  0:00 / 0:00