Skip to content

Number 实例方法

toFixed

toFixed() 方法使用定点表示法来格式化该数值。

语法:

js
toFixed()
toFixed(digits)
示例
js
const numObj = 12345.6789

numObj.toFixed() // '12346';四舍五入,没有小数部分
numObj.toFixed(1) // '12345.7';向上舍入
numObj.toFixed(6) // '12345.678900';用零补足位数
;(1.23e20).toFixed(2) // '123000000000000000000.00'
;(1.23e-10).toFixed(2) // '0.00'
;(2.34).toFixed(1) // '2.3'
;(2.35).toFixed(1) // '2.4';向上舍入
;(2.55).toFixed(1) // '2.5'
// 它向下舍入,因为它无法用浮点数精确表示,并且最接近的可表示浮点数较小
;(2.449999999999999999).toFixed(1) // '2.5'
// 向上舍入,因为它与 2.45 的差值小于 Number.EPSILON。
// 这个字面量实际上编码和 2.45 相同的数值
;(6.02 * 10 ** 23).toFixed(50) // 6.019999999999999e+23;大数仍然使用指数表示法
;-(2.34).toFixed(1) // -2.3,数字
;(-2.34).toFixed(1) // '-2.3'

toLocaleString

toLocaleString() 方法返回这个数字特定于语言环境的表示字符串。在

语法:

js
toLocaleString()
toLocaleString(locales)
toLocaleString(locales, options)
示例
js
const number = 3500

console.log(number.toLocaleString()) // "3,500",如果区域设置为美国英语

基于 MIT 许可发布