Numbers
To write numbers with many zeroes:
- Append
"e"with the zeroes count to the number. Like:123e6is the same as123with 6 zeroes123000000. - A negative number after
"e"causes the number to be divided by 1 with given zeroes. E.g.123e-6means0.000123(123millionths).
For different numeral systems:
- Can write numbers directly in hex (
0x), octal (0o) and binary (0b) systems. parseInt(str, base)parses the stringstrinto an integer in numeral system with givenbase,2 ≤ base ≤ 36.num.toString(base)converts a number to a string in the numeral system with the givenbase.
For regular number tests:
isNaN(value)converts its argument to a number and then tests it for beingNaNNumber.isNaN(value)checks whether its argument belongs to thenumbertype, and if so, tests it for beingNaNisFinite(value)converts its argument to a number and then tests it for not beingNaN/Infinity/-InfinityNumber.isFinite(value)checks whether its argument belongs to thenumbertype, and if so, tests it for not beingNaN/Infinity/-Infinity
For converting values like 12pt and 100px to a number:
- Use
parseInt/parseFloatfor the “soft” conversion, which reads a number from a string and then returns the value they could read before the error.
For fractions:
- Round using
Math.floor,Math.ceil,Math.trunc,Math.roundornum.toFixed(precision). - Make sure to remember there’s a loss of precision when working with fractions.