JavaScript 数字方法

JavaScript 针对数字提供了很多有用的方法来辅助我们处理数字,比如:转换为其他进制,格式化小数位,转换为整数,转换为小数等。

JavaScript 针对数字提供了很多有用的方法来辅助我们处理数字,比如:转换为其他进制,格式化小数位,转换为整数,转换为小数等。

toString() 方法

数字的 toString() 方法返回一个数字的字符串形式。

所有数字方法都可以用于任何类型的数字(文字、变量或表达式):

let x = 123;
console.log(x.toString()); // 输出 123

toString() 方法还可以接受一个参数,指定要用于数字到字符串的转换的基数(从 2 到 36)。如果未指定参数,则默认值为 10。

let x = 32;
console.log(x.toString(10)); // 输出 32
console.log(x.toString(32)); // 输出 10
console.log(x.toString(16)); // 输出 20
console.log(x.toString(8)); // 输出 40
console.log(x.toString(2)); // 输出 100000

toExponential() 方法

toExponential() 方法以指数表示法返回该数值字符串表示形式。

toExponential() 方法还可以接受一个整数参数,用来指定小数点后有几位数字。默认情况下用尽可能多的位数来显示数字。

let x = 9.656;
x.toExponential(2); // returns 9.66e+0
x.toExponential(4); // returns 9.6560e+0
x.toExponential(6); // returns 9.656000e+0

toFixed() 方法

toFixed() 返回一个字符串,参数指定小数的位数。

let x = 9.656;
console.log("x.toFixed(0) :", x.toFixed(0)); // 输出 x.toFixed(0) : 10
console.log("x.toFixed(2) :", x.toFixed(2)); // 输出 x.toFixed(2) : 9.66
console.log("x.toFixed(4) :", x.toFixed(4)); // 输出 x.toFixed(4) : 9.6560
console.log("x.toFixed(6) :", x.toFixed(6)); // 输出 x.toFixed(6) : 9.656000

toFixed(2) 非常适合显示金额。

toPrecision() 方法

toPrecision() 返回一个字符串,参数指定了有效数字的位数。

let x = 9.656;
console.log("x.toPrecision(0) :", x.toPrecision()); // 输出 x.toFixed(0) : 9.656
console.log("x.toPrecision(2) :", x.toPrecision(2)); // 输出 x.toFixed(2) : 9.7
console.log("x.toPrecision(4) :", x.toPrecision(4)); // 输出 x.toFixed(4) : 9.656
console.log("x.toPrecision(6) :", x.toPrecision(6)); // 输出 x.toFixed(6) : 9.65600

转换为数字

JavaScript 中有 3 个全局方法可以非数字类型转换为数字类型:

  • Number() :将参数转为数字,如果无法转换为数字返回 NaN
  • parseInt() :解析一个字符串并返回一个整数,如果无法转换为数字返回 NaN
  • parseFloat() : 解析一个字符串并返回一个数字,如果无法转换为数字返回 NaN

全局方法的作用域是任何地方,可以随时使用。

Number() 方法

Number() 可用于参数转换为数字,如果无法转换为数字返回 NaN

console.log("Number(true)    :", Number(true)); // 输出 1
console.log("Number(false)   :", Number(false)); // 输出 0
console.log('Number("10")    :', Number("10")); // 输出 10
console.log('Number("  10")  :', Number("  10")); // 输出 10
console.log('Number("10  ")  :', Number("10  ")); // 输出 10
console.log('Number(" 10  ") :', Number(" 10  ")); // 输出 10
console.log('Number("10.33") :', Number("10.33")); // 输出 10.33
console.log('Number("10,33") :', Number("10,33")); // 输出 NaN
console.log('Number("10 33") :', Number("10 33")); // 输出 NaN
console.log('Number("John")  :', Number("John")); // 输出 NaN

parseInt() 方法

parseInt() 解析一个字符串并返回一个整数。如果无法转换为数字返回 NaN。如果参数有空格。只返回第一个数字。

console.log('parseInt("-10")      :', parseInt("-10")); // 输出 parseInt("-10")      : -10
console.log('parseInt("-10.33")   :', parseInt("-10.33")); // 输出 parseInt("-10.33")   : -10
console.log('parseInt("10")       :', parseInt("10")); // 输出 parseInt("10")       : 10
console.log('parseInt("10.33")    :', parseInt("10.33")); // 输出 parseInt("10.33")    : 10
console.log('parseInt("10 20 30") :', parseInt("10 20 30")); // 输出 parseInt("10 20 30") : 10
console.log('parseInt("10 years") :', parseInt("10 years")); // 输出 parseInt("10 years") : 10
console.log('parseInt("years 10") :', parseInt("years 10")); // 输出 parseInt("years 10") : NaN

parseFloat() 方法

parseFloat() 解析一个字符串并返回一个数字。如果无法转换为数字返回 NaN。如果参数有空格。只返回第一个数字。

console.log('parseFloat("10")       :', parseFloat("10")); // 输出 parseFloat("10")       : 10
console.log('parseFloat("10.33")    :', parseFloat("10.33")); // 输出 parseFloat("10.33")    : 10.33
console.log('parseFloat("10 20 30") :', parseFloat("10 20 30")); // 输出 parseFloat("10 20 30") : 10
console.log('parseFloat("10 years") :', parseFloat("10 years")); // 输出 parseFloat("10 years") : 10
console.log('parseFloat("years 10") :', parseFloat("years 10")); // 输出 parseFloat("years 10") : NaN

常用属性

数字类 Number 提供了一些公共属性来描述一些特别的值。

属性 说明
MAX_VALUE JavaScript 中的最大数值
MIN_VALUE JavaScript 中的最小数值
POSITIVE_INFINITY 正无穷大
NEGATIVE_INFINITY 负无穷大
NaN 代表非数字

这几个属性都是 Number 类的静态属性,可以通过类名直接调用。

Number.MAX_VALUE

let x = Number.MAX_VALUE;
console.log("Number.MAX_VALUE =", x); // 输出 Number.MAX_VALUE = 1.7976931348623157e+308

Number.MIN_VALUE

let x = Number.MIN_VALUE;
console.log("Number.MIN_VALUE =", x); // 输出 Number.MIN_VALUE = 5e-324

Number.POSITIVE_INFINITY

const x = Number.POSITIVE_INFINITY;
console.log("Number.POSITIVE_INFINITY =", x); // 输出 Number.POSITIVE_INFINITY = Infinity
const y = 1 / 0;
console.log("1 / 0 =", y); // 输出 1 / 0 = Infinity

Number.NEGATIVE_INFINITY

const x = Number.NEGATIVE_INFINITY;
console.log("Number.NEGATIVE_INFINITY =", x); // 输出 Number.NEGATIVE_INFINITY = -Infinity
const y = -1 / 0;
console.log("-1 / 0 =", y); // 输出 -1 / 0 = -Infinity