Problem: Multiple ways to format data in JS to preserve two-bit decimal functions
The best way:
Keep two bits like that.
Copy the Code code as follows:
var a = 9.39393;
Alert (a.tofixed (2));
Description
Alert (number.tofixed (9.39393));
The return is 9.39.
However, only ie5.5 versions are supported.
Other methods:
function
roundFun(numberRound,roundDigit)
//四舍五入,保留位数为roundDigit
{
if
(numberRound>=0)
{
var
tempNumber = parseInt((numberRound * Math.pow(10,roundDigit)+0.5))/Math.pow(10,roundDigit);
return
tempNumber;
}
else
{
numberRound1=-numberRound
var
tempNumber = parseInt((numberRound1 * Math.pow(10,roundDigit)+0.5))/Math.pow(10,roundDigit);
return
-tempNumber;
}
}
Method Two:
<script>
tmp =
"1234567.57232"
result = tmp.substr(0,tmp.indexOf(
"."
)+3);
alert(result);
</script>
方法三:
var a=3.1415926;
A = a.tofixed (2);//reserved 2 bit but result is a string type
A = parsefloat (a);//convert result to float
In one step, the following
A = Parsefloat (a.tofixed (2));
This article is from the "Liu Bofang blog" blog, make sure to keep this source http://liubofang.blog.51cto.com/11162557/1875830
JS format number reserved Two-bit decimal point example code