let billion = 1e9; // 1 billion, literally: 1 and 9 zeroes
alert( 7.3e9 ); // 7.3 billions (7,300,000,000)
1e3 = 1 * 1000
1.23e6 = 1.23 * 1000000
let ms = 0.000001;
let ms = 1e-6; // six zeroes to the left from 1
// -3 divides by 1 with 3 zeroes
1e-3 = 1 / 1000 (=0.001)
// -6 divides by 1 with 6 zeroes
1.23e-6 = 1.23 / 1000000 (=0.00000123)
Hex, binary and octal numbers
hex 在 JavaScript 被廣泛應用,簡短的縮寫 0x + number 。
alert( 0xff ); // 255
alert( 0xFF ); // 255 (the same, case doesn't matter)
let a = 0b11111111; // binary form of 255
let b = 0o377; // octal form of 255
alert( a == b ); // true, the same number 255 at both sides
toString(base)
num.toString(base) 方法可以將 num 返回 string 用 base 數字系統。
base = 16,16進位,返回 0-9 a-f。
base = 2,2進位,返回 0-1。
base = 36,36進位,返回 0-9 a-z。
let num = 255;
alert( num.toString(16) ); // ff
alert( num.toString(2) ); // 11111111
alert( 123456..toString(36) ); // 2n9c
alert( (123456).toString(36) ); // same
Rounding
Math.floor:無條件退位
Math.ceil:無條件進位
Math.round:四捨五入
Math.trunc (not supported by Internet Explorer):無條件捨去
Math.floor
Math.ceil
Math.round
Math.trunc
3.1
3
4
3
3
3.6
3
4
4
3
-1.1
-2
-1
-1
-1
-1.6
-2
-1
-2
-1
以上都是進位到整數的方法,若需要盡味道特定小數點後幾位,有 2 種方法:
Multiply-and-divide.
let num = 1.23456;
alert( Math.floor(num * 100) / 100 ); // 1.23456 -> 123.456 -> 123 -> 1.23
toFixed(n) 方法四捨五入數字返回 string 在用 Number()、+返回數字。
let num = 12.34;
alert( num.toFixed(1) ); // "12.3"
let num = 12.36;
alert( num.toFixed(1) ); // "12.4"
let num = 12.34;
alert( num.toFixed(5) ); // "12.34000", added zeroes to make exactly 5 digits
alert( isNaN(NaN) ); // true
alert( isNaN("str") ); // true
alert( NaN === NaN ); // false
isFinite(value) 轉換參數為 number 檢測是否為 Infinity ,返回布林值。
alert( isFinite("15") ); // true
alert( isFinite("str") ); // false, because a special value: NaN
alert( isFinite(Infinity) ); // false, because a special value: Infinity
let num = +prompt("Enter a number", '');
// will be true unless you enter Infinity, -Infinity or not a number
alert( isFinite(num) );
parseInt and parseFloat
通常在類型轉換的時候數字後有單位,像是 '100px'、'12pt',這時要使用 parseInt and parseFloat。
// 一般的轉換無法忽略單位
alert( +"100px" ); // NaN
// parseInt and parseFloat 讀取數字直到非數字
alert( parseInt('100px') ); // 100
alert( parseFloat('12.5em') ); // 12.5
// parseInt 返回整數,paseFloat 返回小數
alert( parseInt('12.3') ); // 12, only the integer part is returned
alert( parseFloat('12.3.4') ); // 12.3, the second point stops the reading
// 一開始就是非數字無法讀取
alert( parseInt('a123') ); // NaN, the first symbol stops the process
// parseInt(str, radix) 第 2 個參數設定讀取的數字系統
alert( parseInt('0xff', 16) ); // 255
alert( parseInt('ff', 16) ); // 255, without 0x also works
alert( parseInt('2n9c', 36) ); // 123456