Home >Web Front-end >JS Tutorial >js format decimal
The best way:
Keep two digits like this
var a = 9.39393;
alert(a.toFixed(2));
Explanation:
alert(Number.toFixed(9.39393));
Returned It is 9.39
But it is only supported by versions above IE5.5.
Other methods:
Method 1:
function roundFun(numberRound,roundDigit) //Rounding, the number of reserved digits is roundDigit {
if (numberRound>=0)
var temp Number = 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 2: