toFixed

toFixed

> 函数列表

返回一个字符串,内容是数字 x* 保留小数点后恰好 *digits 位的写法:toFixed(1.0, 2) 返回 "1.00",而 string 给出 "1.0"。

数字按最接近的值舍入,恰好一半时向远离零的方向舍入:toFixed(2.5, 0) 返回 "3",toFixed(-2.5, 0) 返回 "-3"。舍入针对 string 写出的那个数字,因此 toFixed(1.005, 2) 返回 "1.01"。结果总是用点作小数点,从不使用科学计数法,也不会出现 "-0.00"。

toFixed(1.0, 2)          // "1.00"
toFixed(3.14159, 2)      // "3.14"
toFixed(2.999, 2)        // "3.00"
toFixed(0.1 + 0.2, 2)    // "0.30"
toFixed(12345678.9, 2)   // "12345678.90"
toFixed(-0.004, 2)       // "0.00"
toFixed(7, 3)            // "7.000"
(1.0).toFixed(2);      // "1.00"
(3.14159).toFixed(2);  // "3.14"
(1.005).toFixed(2);    // "1.00"
(-0.004).toFixed(2);   // "-0.00"
f"{1.0:.2f}"            # '1.00'
f"{3.14159:.2f}"        # '3.14'
format(1.005, ".2f")    # '1.00'
format(0.125, ".2f")    # '0.12'
参数
返回值
注意