小数点后可获得2个值 [英] get 2 vales after decimal
本文介绍了小数点后可获得2个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用javascript
如何在小数点后获取2个值
(例如)
它给我这样的结果77.888555
我想要这样的77.88
在此先感谢
i m using javascript
how to get 2 values after decimal
(eg)
Its giving me result like this 77.888555
I want like this 77.88
Thanks in advance
推荐答案
和 ^ ]从来没有出现过?
And this[^] never occured to you ?
尝试一下
四舍五入:
Try this
To round off:
Math.round(77.888555*100)/100 ;
输出=> 77.89
要截断试试这个
Output => 77.89
To truncate try this
var n=77.888555;
alert(truncate(n));
function truncate(num)
{
var returnValue=num.toString();
var pos = returnValue.indexOf(".");
if ( pos == -1 )
{
return returnValue + ".00";
}
else
{
returnValue += "00";
return returnValue.substr(0,pos+3);
}
}
输出=> 77.88
Output => 77.88
number.toFixed(x);
x:可选.小数点后的位数.默认值为0(小数点后没有数字)
x: Optional. The number of digits after the decimal point. Default is 0 (no digits after the decimal point)
这篇关于小数点后可获得2个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文