给我一个 native (请不要使用 jQuery、Prototype 等)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
.谢谢你。
最佳答案
input = 0.3;
output = input.toFixed(2);
//output: 0.30
关于javascript - 将数字格式化为两位小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4610298/