请给我一个原生的(没有jQuery,原型等) JavaScript函数,它可以按如下方式转换数字:
input: 0.39, 2.5, 4.25, 5.5, 6.75, 7.75, 8.5
output: 0.39, 2.50, 4.25, 5.50, 6.75, 7.75, 8.50
例如,在Ruby中,我会这样做:
>> sprintf("%.2f", 2.5)
=> "2.50"
输出可以是数字或字符串。我真的不关心,因为我只是用它来设置innerHTML
。
谢谢。
发布于 2011-01-06 07:29:50
input = 0.3;
output = input.toFixed(2);
//output: 0.30
发布于 2011-01-06 07:28:51
发布于 2011-01-06 07:28:51
使用小数位数为2的toFixed
。
https://stackoverflow.com/questions/4610298
复制相似问题