nStr = 12.00;
alert(typeof(nStr));// getting number
x = nStr.split('.');
// Other than string Split function Through an error,
nStr = 12.00;
nStr += '';
alert(typeof(nStr));// getting string
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
alert(x1+x2);
//Expected Output is 12.00
Run Code Online (Sandbox Code Playgroud)
我得到了临时输出编码(它没有优化编码)
nStr += '';
x = nStr.split('.');
x1 = x[0];
x[1]= (x[1].length == 1) ? '.' + x[1] + '0' : '.' + x[1];
x2 = x.length > 1 ? x[1] : '.00';
alert(x1+x2); // 12.00
Run Code Online (Sandbox Code Playgroud)
12.00 是 12你不能改变那个事实.
看起来你所追求的是toFixed()JavaScript 的方法:
nStr = 12;
alert(nStr.toFixed(2));
Run Code Online (Sandbox Code Playgroud)
这将根据需要提醒"12.00".传递给方法的数字决定了小数点后显示的位数.
值得一提的是,该toFixed方法还将对数字进行舍入,例如,如果在上面的示例nStr中将是12.5283,则它将在警报中显示12.53.
| 归档时间: |
|
| 查看次数: |
2715 次 |
| 最近记录: |