ma1*_*w28 18 javascript numbers number-formatting
给我一个原生(没有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
Run Code Online (Sandbox Code Playgroud)
例如,在Ruby中,我会做这样的事情:
>> sprintf("%.2f", 2.5)
=> "2.50"
Run Code Online (Sandbox Code Playgroud)
输出可以是数字或字符串.我真的不在乎,因为我只是用它来设置innerHTML.
谢谢.
Eri*_*tis 32
input = 0.3;
output = input.toFixed(2);
//output: 0.30
Run Code Online (Sandbox Code Playgroud)
var array = [0.39, 2.5, 4.25, 5.5, 6.75, 7.75, 8.5], new_array = [];
for(var i = 0, j = array.length; i < j; i++) {
if(typeof array[i] !== 'number') continue;
new_array.push(array[i].toFixed(2));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28641 次 |
| 最近记录: |