Number 對象
在 JavaScript 中, Number 是一種基本的數據類型,它是原始數值的包裝對象。在必要時,JavaScript 會自動地把原始數值轉化成 Number 對象。
構造函數 Number() 可以不與運算符 new 一起使用,而直接作為轉化函數來使用。以這種方式調用 Number() 時,它會把自己的參數轉化成一個數字,然后返回轉換后的原始數值(或 NaN)。
它提供了處理數值的相關操作。
創建一個 Number 實例對象的方法:
- var num = Number(123)
- var num = new Number(123)
- var num = 123-""
Number 對象與其實例對象的屬性和方法:
// Object.getOwnPropertyDescriptors(Number): name : {value: "Number", writable: false, enumerable: false, configurable: true} length : {value: 1, writable: false, enumerable: false, configurable: true} prototype : {value: Number, writable: false, enumerable: false, configurable: false} EPSILON : {value: 2.220446049250313e-16, writable: false, enumerable: false, configurable: false} MAX_SAFE_INTEGER : {value: 9007199254740991, writable: false, enumerable: false, configurable: false} MAX_VALUE : {value: 1.7976931348623157e+308, writable: false, enumerable: false, configurable: false} MIN_SAFE_INTEGER : {value: -9007199254740991, writable: false, enumerable: false, configurable: false} MIN_VALUE : {value: 5e-324, writable: false, enumerable: false, configurable: false} NEGATIVE_INFINITY : {value: -Infinity, writable: false, enumerable: false, configurable: false} NaN : {value: NaN, writable: false, enumerable: false, configurable: false} POSITIVE_INFINITY : {value: Infinity, writable: false, enumerable: false, configurable: false} isFinite : {writable: true, enumerable: false, configurable: true, value: ƒ} isInteger : {writable: true, enumerable: false, configurable: true, value: ƒ} isNaN : {writable: true, enumerable: false, configurable: true, value: ƒ} isSafeInteger : {writable: true, enumerable: false, configurable: true, value: ƒ} parseFloat : {writable: true, enumerable: false, configurable: true, value: ƒ} parseInt : {writable: true, enumerable: false, configurable: true, value: ƒ} // Object.getOwnPropertyDescriptors(Number.prototype): constructor : {writable: true, enumerable: false, configurable: true, value: ƒ} toString : {writable: true, enumerable: false, configurable: true, value: ƒ} toLocaleString : {writable: true, enumerable: false, configurable: true, value: ƒ} valueOf : {writable: true, enumerable: false, configurable: true, value: ƒ} toExponential : {writable: true, enumerable: false, configurable: true, value: ƒ} toFixed : {writable: true, enumerable: false, configurable: true, value: ƒ} toPrecision : {writable: true, enumerable: false, configurable: true, value: ƒ}
Number 方法與描述
方法 | 描述 |
---|---|
isFinite() | 判斷是否一個有窮數 |
isInteger() | 判斷是否一個整數 |
isNaN() | 判斷是否一個NaN |
isSafeInteger() | 判斷是否安全整數 |
parseFloat() | 把一個值解析為浮點數 |
parseInt() | 把一個值解析為整數 |
Number 常用方法
1.isFinite()
功能:判斷是否有窮數。
語法:Number.isFinite(value)
參數:value,被檢測是否有窮性的值
返回值:true 或 false
示例:
Number.isFinite(Infinity); // false Number.isFinite(NaN); // false Number.isFinite(-Infinity); // false Number.isFinite(0); // true Number.isFinite(2e64); // true // 注意:全局函數 isFinite('0') 會返回 true Number.isFinite('0'); // false
2.isInteger()
功能:判斷是否整數。
語法:Number.isInteger(value)
參數:value,被檢測是否整數的值
返回值:true 或 false
示例:
Number.isInteger(0); // true Number.isInteger(111111); // true Number.isInteger(-233333); // true Number.isInteger(0.0001111); // false Number.isInteger(Math.PI); // false Number.isInteger("10"); // false Number.isInteger([1]); // false Number.isInteger({a:1}); // false Number.isInteger(()=>1); // false Number.isInteger(Infinity); // false Number.isInteger(-Infinity); // false Number.isInteger(NaN); // false Number.isInteger(undefined); // false Number.isInteger(true); // false Number.isInteger(false); // false
3.isNaN()
功能:判斷是否NaN。
語法:Number.isNaN(value)
參數:value,被檢測是否NaN的值
返回值:true 或 false
示例:
Number.isNaN(NaN); // true Number.isNaN(Number.NaN); // true Number.isNaN(0 / 0) // true // 下面這幾個如果使用全局的 isNaN() 時,會返回 true。 Number.isNaN("NaN"); // false,字符串 "NaN" 不會被隱式轉換成數字 NaN。 Number.isNaN(undefined); // false Number.isNaN({}); // false Number.isNaN("blabla"); // false // 下面的都返回 false Number.isNaN(true); Number.isNaN(null); Number.isNaN(37); Number.isNaN("37"); Number.isNaN("37.37"); Number.isNaN(""); Number.isNaN(" ");
4.isSafeInteger()
功能:判斷是否安全整數。可以認為安全整數就是 -9007199254740991 ~ 9007199254740991的整數
語法:Number.isSafeInteger(value)
參數:value,被檢測是否安全整數的值
返回值:true 或 false
示例:
Number.isSafeInteger( 1); // true Number.isSafeInteger( 0); // true Number.isSafeInteger(-1); // true Number.isSafeInteger("1"); // false Number.isSafeInteger(1.1); // false Number.isSafeInteger(1.0); // true Number.isSafeInteger(Math.pow(2, 53) - 1) // true Number.isSafeInteger(Math.pow(2, 53)) // false Number.isSafeInteger(-(Math.pow(2, 53) - 1)) // true Number.isSafeInteger(-Math.pow(2, 53)) // false Number.isSafeInteger( 9007199254740991); // true Number.isSafeInteger( 9007199254740992); // false Number.isSafeInteger(-9007199254740991); // true Number.isSafeInteger(-9007199254740992); // false Number.isSafeInteger(NaN); // false Number.isSafeInteger(Infinity); // false
5.parseFloat()
功能:
- 把值解析為浮點數。
- 解析過程中遇到了正號(+)、負號(-)、數字(0-9)、小數點(.)、或者科學記數法中的指數(e 或 E)以外的字符,則它會忽略該字符以及之后的所有字符,返回當前已經解析到的浮點數。
- 第二個小數點的出現也會使解析停止(在這之前的字符都會被解析)。
- 參數首位和末位的空白符會被忽略。
- 如果參數字符串的第一個字符不能被解析成為數字,則返回 NaN。
- parseFloat 也可以解析並返回 Infinity。
- parseFloat解析 BigInt 為 Numbers, 丟失精度。因為末位 n 字符被丟棄。
語法:Number.parseFloat(string)
參數:value,被解析的值。
返回值:浮點數或 NaN
示例:
// 以下示例都返回3.14 Number.parseFloat(3.14); Number.parseFloat('3.14'); Number.parseFloat(' 3.14 '); Number.parseFloat('3.14.15'); Number.parseFloat('314e-2'); Number.parseFloat('0.0314E+2'); Number.parseFloat('3.14todotodotodo'); Number.parseFloat({ toString: function() { return "3.14" } }); // 以下示例返回NaN parseFloat("ABCD233");
6.parseInt()
功能:把值解析為整數。
語法:Number.parseInt(string[, radix])
參數:
- string:要解析的值。 如果此參數不是字符串,則使用ToString抽象操作將其轉換為字符串。忽略此參數中的前導空格。
- radix:一個介於2到36之間的整數,代表字符串的基數(數學數字系統中的基)。
- 如果輸入的 string以 "0x"或 "0x"(一個0,后面是小寫或大寫的X)開頭,那么radix被假定為16,字符串的其余部分被解析為十六進制數。
- 如果輸入的 string以 "0"(0)開頭, radix被假定為8(八進制)或10(十進制)。具體選擇哪一個radix取決於實現。ECMAScript 5 澄清了應該使用 10 (十進制),但不是所有的瀏覽器都支持。因此,在使用 parseInt 時,一定要指定一個 radix。
- 如果輸入的 string 以任何其他值開頭, radix 是 10 (十進制)。
返回值:整數或 NaN
示例:
// 以下例子均返回15: parseInt("0xF", 16); parseInt("F", 16); parseInt("17", 8); parseInt(021, 8); parseInt("015", 10); // parseInt(015, 10); 返回 13 parseInt(15.99, 10); parseInt("15,123", 10); parseInt("FXX123", 16); parseInt("1111", 2); parseInt("15 * 3", 10); parseInt("15e2", 10); parseInt("15px", 10); parseInt("12", 13); // 以下例子均返回 -15: parseInt("-F", 16); parseInt("-0F", 16); parseInt("-0XF", 16); parseInt(-15.1, 10); parseInt(" -17", 8); parseInt(" -15", 10); parseInt("-1111", 2); parseInt("-15e1", 10); parseInt("-12", 13); // 以下例子均返回 NaN: parseInt("Hello", 8); // 根本就不是數值 parseInt("546", 2); // 除了“0、1”外,其它數字都不是有效二進制數字
Number.prototype 常用方法與描述
方法 | 描述 |
---|---|
toExponential() | 強制將數值以指數形式顯示。 |
toFixed() | 可把 Number 四舍五入為指定小數位數的數字。 |
toPrecision() | 定義顯示一個數多少位數(包括位小數的左和右) |
Number.prototype 常用方法
1.toFixed()
功能:使用定點(四舍五入)表示法來格式化一個數值。
語法:num.toFixed(digits)。
參數:digits,小數點后數字的個數;介於 0 到 20 (包括)之間,實現環境可能支持更大范圍。如果忽略該參數,則默認為 0。
返回值:根據 digits 四舍五入后的字符串。
示例:
var num = 12345.6789; num.toFixed(); // 返回 "12346":進行四舍六入五看情況,不包括小數部分 num.toFixed(1); // 返回 "12345.7":進行四舍六入五看情況 num.toFixed(6); // 返回 "12345.678900":用0填充 (1.23e+20).toFixed(2); // 返回 "123000000000000000000.00" (1.23e-10).toFixed(2); // 返回 "0.00" 2.34.toFixed(1); // 返回 "2.3" 2.35.toFixed(1) // 返回 '2.4'. Note it rounds up 2.55.toFixed(1) // 返回 '2.5'. Note it rounds down - see warning above -2.34.toFixed(1); // 返回 -2.3 (由於操作符優先級,負數不會返回字符串) (-2.34).toFixed(1); // 返回 "-2.3" (若用括號提高優先級,則返回字符串)
2.toExponential()
功能:以指數表示法返回該數值字符串表示形式。
語法:numObj.toExponential(fractionDigits)
參數:fractionDigits,一個整數,用來指定小數點后有幾位數字。默認情況下用盡可能多的位數來顯示數字。
返回值:一個科學記數法字符串。
示例:
var num = 77.1234; num.toExponential(); // 7.71234e+1 num.toExponential(4); // 7.7123e+1 num.toExponential(2); // 7.71e+177.1234.toExponential(); // 7.71234e+177 .toExponential(); // 7.7e+1
3.toPrecision()
功能:以指定的精度(四舍五入)返回該數值對象的字符串表示。
語法:num.toPrecision(precision)
參數:precision,一個用來指定有效數個數的整數。
返回值:以定點表示法或指數表示法表示的一個數值對象的字符串。
示例:
var num = 123.456789; num.toPrecision(); // "123.456789" num.toPrecision(1); // "1e+2" num.toPrecision(2); // "1.2e+2" num.toPrecision(3); // "123" num.toPrecision(4); // "123.5" num.toPrecision(5); // "123.46" num.toPrecision(10); // "123.4567890" num.toPrecision(15); // "123.456789000000"
JavaScript 中的三大對象 (本地對象、內置對象、 宿主對象)
本地對象
- Object 對象屬性和方法
- String 對象屬性和方法
- Array 對象屬性和方法
- Date 對象屬性和方法
- Number 對象屬性和方法
- RegExp 對象屬性和方法
- Function 對象屬性和方法
- Boolean 對象屬性和方法
- Error 對象屬性和方法
內置對象
宿主對象