Number的方法
Number.parseInt() 重点!
Number.parseInt(string, radix)
解析一个字符串并返回指定基数的十进制整数, radix
是2-36之间的整数,表示被解析字符串的基数。
参数
string
:要被解析的值。如果参数不是一个字符串,则将其转换为字符串(使用 ToString 抽象操作)。字符串开头的空白符将会被忽略。
radix
(可选):从 2 到 36,表示字符串的基数。例如指定 16 表示被解析值是十六进制数。
请注意,10不是默认值。
如果 radix
是 undefined
、0
或未指定的,JavaScript会假定以下情况:
- 如果输入的
string
以 "0x
"或 “0X
”(一个0,后面是小写或大写的X)开头,那么radix被假定为16,字符串的其余部分被当做十六进制数去解析。 - 如果输入的
string
以 “0
”(0)开头,radix
被假定为8
(八进制)或10
(十进制)。具体选择哪一个radix取决于实现。ECMAScript 5 澄清了应该使用 10 (十进制),但不是所有的浏览器都支持。因此,在使用parseInt
时,一定要指定一个 radix。 - 如果输入的
string
以任何其他值开头,radix
是10
(十进制)。
如果第一个字符不能转换为数字,parseInt
会返回 NaN
。
//以下例子均返回15
Number.parseInt("0xF", 16);
Number.parseInt("F", 16);
Number.parseInt("17", 8);
Number.parseInt(021, 8);
Number.parseInt("015", 10); // parseInt(015, 8); 返回 13
Number.parseInt(15.99, 10);
Number.parseInt("15,123", 10);
Number.parseInt("FXX123", 16);
Number.parseInt("1111", 2);
Number.parseInt("15 * 3", 10);
Number.parseInt("15e2", 10);
Number.parseInt("15px", 10);
Number.parseInt("12", 13);
//以下例子均返回-15
Number.parseInt("-F", 16);
Number.parseInt("-0F", 16);
Number.parseInt("-0XF", 16);
Number.parseInt(-15.1, 10);
Number.parseInt(" -17", 8);
Number.parseInt(" -15", 10);
Number.parseInt("-1111", 2);
Number.parseInt("-15e1", 10);
Number.parseInt("-12", 13);
//————————————————————————
Number.parseInt("0e0",16); //224
//以下例子均返回NaN
Number.parseInt("Hello", 8); // 根本就不是数值
Number.parseInt("546", 2); // 除了“0、1”外,其它数字都不是有效二进制数字
Number.parseInt(4.7, 10); //4
//特殊情况
Number.parseInt(4.7 * 1e22, 10); // 非常大的数值变成 4
Number.parseInt(0.00000000000434, 10); // 非常小的数值变成 4
返回值
从给定的字符串中解析出的一个整数。
或者 NaN
,当radix 小于2或大于36第一个非空格字符不能转换为数字。
Number.parseFloat()
Number.parseFloat()
方法可以把一个字符串解析成浮点数。该方法与全局的 parseFloat() 函数相同,并且处于 ECMAScript 6 规范中(用于全局变量的模块化)。
//以下例子都返回3.14
Number.parseFloat(3.14);
Number.parseFloat('3.14');
Number.parseFloat(' 3.14 ');
Number.parseFloat('314e-2');
Number.parseFloat('0.0314E+2');
Number.parseFloat('3.14some non-digit characters');
Number.parseFloat({ toString: function() { return "3.14" } });
//以下例子返回NaN
Number.parseFloat("FF2");
//以下例子均返回 900719925474099300,当整数太大以至于不能被转换时将失去精度。
Number.parseFloat(900719925474099267n);
Number.parseFloat('900719925474099267n');
返回值
给定值被解析成浮点数,如果无法被解析成浮点数,则返回NaN
Number.isFinite()
Number.isFinite()
方法用来检测传入参数是否是一个有穷数。
console.log(Number.isFinite(1/0)); // false
console.log(Number.isFinite(10/5)); // true
console.log(Number.isFinite(0/0)); // false
console.log(Number.isFinite(NaN)); // false
console.log(Number.isFinite(Infinity)) // false
console.log(Number.isFinite(-Infinity)) // false
console.log(Number.isFinite(Math.PI)) // true
Number.isFinite('0'); // false
// 在全局的isFinite('0')会返回true
Number.isFinite(null); // false
// 在全局的isFinite(null)会返回true
值得一提的是Math.PI在数学中是一个无理数,但在js中被处理成为了3.141592653589793,所以他也是一个有穷数。
返回值
一个布尔值
表示给定的值是否是一个有穷数。
Number.isInteger()
Number.isInteger()
方法用来判断参数是否是整数。
Number.isInteger(0); // true
Number.isInteger(1); // true
Number.isInteger(-100000); // true
Number.isInteger(0.1); // false
Number.isInteger(Math.PI); // false
Number.isInteger(Infinity); // false
Number.isInteger(-Infinity); // false
Number.isInteger("10"); // false
Number.isInteger(true); // false
Number.isInteger(false); // false
Number.isInteger([1]); // false
Number.isInteger(10.000) // true
返回值
一个布尔值
表示给定的值是否是一个整数。
Number.isSafeInteger()
Number.isSafeInteger()
方法用来判断传入的参数值是否是一个“安全整数”(safe integer)。
安全整数范围为 -(2^53 - 1)到
2^53 - 1
之间的整数,包含 -(2^53 - 1)和
2^53 - 1
。
Number.isSafeInteger(3); // true
Number.isSafeInteger(Math.pow(2, 53)) // false
Number.isSafeInteger(Math.pow(2, 53) - 1) // true
Number.isSafeInteger(NaN); // false
Number.isSafeInteger(Infinity); // false
Number.isSafeInteger("3"); // false
Number.isSafeInteger(3.1); // false
Number.isSafeInteger(3.0); // true
返回值
一个布尔值
表示给定的值是否是一个安全整数。
以上是Number数据类型的方法,以下是Number类型原型对象上的方法
Number.prototype.toFixed()
num.toFixed()
是一种数值格式化为字符串的方法,()中可以接收参数,来表示格式化为指定小数点位数,如果不传,默认为0,如果数值本身的小数位超过了参数指定的位数,则四舍五入到最接近的小数位。
let num = 10;
console.log(num.toFixed(2)); // "10.00"
let num2 = 10.005
console.log(num2.toFixed()); // "10"
console.log(num2.toFixed(2)); // "10.01"
toFixed()自动舍入的特点可以用于处理货币。不过要注意的是,多个浮点数值的数学计算不一定
得到精确的结果。比如,0.1 + 0.2 = 0.30000000000000004。
注意:toFixed()方法可以表示有 0~20 个小数位的数值。某些浏览器可能支持更大的范
围,但这是通常被支持的范围。
返回值
使用定点表示法表示给定数字的字符串。
Number.prototype.toExponential()
num.toExponential(fractionDigits)
返回以科学记数法表示的数值字符串。与 toFixed一样,toExponential也接收一个参数,表示结果中小数的位数。参数如果不传则用尽可能多的位数来显示数字。
let num = 10.1234;
console.log(num.toExponential()); //输出 1.01234e+1
console.log(num.toExponential(4)); //输出 1.0123e+1
console.log(num.toExponential(2)); //输出 1.01e+1
console.log(10.1234.toExponential()); //输出 1.01234e+1
console.log(10 .toExponential()); //输出 7.7e+1
let num = 10;
console.log(num.toExponential(1)); // "1.0e+1"
这段代码的输出为"1.0e+1"。一般来说,这么小的数不用表示为科学记数法形式。如果想得到数
值最适当的形式,那么可以使用 toPrecision。
返回值
使用科学记数法表示的数值字符串。
Number.prototype.toPrecision()
num.toPrecision(precision)
方法会根据情况返回以定点表示法或指数表示法表示的一个数值对象的字符串表示,四舍五入到 precision
参数指定的显示数字位数。
形式。这个方法接收一个参数,表示结果中数字的总位数(不包含指数)。
let num = 99;
console.log(num.toPrecision(1)); // "1e+2"
console.log(num.toPrecision(2)); // "99"
console.log(num.toPrecision(3)); // "99.0"
首先要用 1 位数字表示数值 99,得到"1e+2",也就是 100。因为 99 不能只用 1 位
数字来精确表示,所以这个方法就将它舍入为 100,这样就可以只用 1 位数字(及其科学记数法形式)
来表示了。用 2 位数字表示 99 得到"99",用 3 位数字则是"99.0"。
返回值
使用定点表示法或科学记数法表示的数值字符串。
小结
本质上,toPrecision()方法会根据数值和精度来决定调用 toFixed()还是 toExponential()。为了以正确的小数位精确表示数值,这 3 个方法都会向上或向下舍入。
Number.prototype.balueOf()
num.valueOf()
方法返回一个被Number对象包装的原始值。
var num = new Number(10);
console.log(typeof num); // object
var num2 = num.valueOf();
console.log(num2); // 10
console.log(typeof num2); // number
返回值
一个类型为Number的原始值。
Number.prototype.toString()
num.toString(radix)
方法返回指定Number对象的字符串表示形式,方法接收一个参数radix指定要用于数字到字符串的转换的基数(从2到36)。如果未指定radix参数,则默认值为10。
var count = 10;
console.log(count.toString()); // 输出 '10'
console.log((17).toString()); // 输出 '17'
console.log((17.2).toString()); // 输出 '17.2'
var x = 6;
console.log(x.toString(2)); // 输出 '110'
console.log((254).toString(16)); // 输出 'fe'
console.log((-10).toString(2)); // 输出 '-1010'
console.log((-0xff).toString(2)); // 输出 '-11111111'
如果对象是负数,则会保留负号。即使radix是2时也是如此:返回的字符串包含一个负号(-)前缀和正数的二进制表示,不是 数值的二进制补码。
进行数字到字符串的转换时,建议用小括号将要转换的目标括起来,防止出错。
返回值
数值根据指定基数转换后的字符串。
Number.prototype.toLocaleString() 不常用
num.toLocaleString()
方法返回这个数字在特定语言环境下的表示字符串。
var number = 3500;
console.log(number.toLocaleString()); // 打印"3,500",如果在U.S. English场景下
返回值
字符串。
此处不过多介绍,可参阅MDN文档:toLocaleString()
个人整理,觉得有用求个点赞收藏,感谢!!!