Number 常用方法汇总

摘要:toExponential()把对象的值转换为指数计数法。toFixed()把数字转换为字符串,并对小数点指定位数。toLocaleString()把数字转换为字符串,使用本地数字格式顺序。

toExponential()

// 把对象的值转换为指数计数法。
var num1 = 1225.30
var val = num1.toExponential();
console.log(val) // 输出: 1.2253e+3

toFixed()

// 把数字转换为字符串,并对小数点指定位数。
var num3 = 177.234
console.log("num3.toFixed() 为 "+num3.toFixed())    // 输出:177
console.log("num3.toFixed(2) 为 "+num3.toFixed(2))  // 输出:177.23
console.log("num3.toFixed(6) 为 "+num3.toFixed(6))  // 输出:177.234000

toLocaleString()

// 把数字转换为字符串,使用本地数字格式顺序。
var num = new Number(177.1234);
console.log( num.toLocaleString());  // 输出:177.1234

toPrecision()

// 把数字格式化为指定的长度。
var num = new Number(7.123456);
console.log(num.toPrecision());  // 输出:7.123456
console.log(num.toPrecision(1)); // 输出:7
console.log(num.toPrecision(2)); // 输出:7.1

toString()

// 把数字转换为字符串,使用指定的基数。数字的基数是 2 ~ 36 之间的整数。若省略该参数,则使用基数 10。
var num = new Number(10);
console.log(num.toString());  // 输出10进制:10
console.log(num.toString(2)); // 输出2进制:1010
console.log(num.toString(8)); // 输出8进制:12

valueOf()

// 返回一个 Number 对象的原始数字值。
var num = new Number(10);
console.log(num.valueOf()); // 输出:10

本文内容仅供个人学习、研究或参考使用,不构成任何形式的决策建议、专业指导或法律依据。未经授权,禁止任何单位或个人以商业售卖、虚假宣传、侵权传播等非学习研究目的使用本文内容。如需分享或转载,请保留原文来源信息,不得篡改、删减内容或侵犯相关权益。感谢您的理解与支持!

链接: https://shenqiku.cn/article/FLY_8952