Home > Article > Web Front-end > JS format number retaining two decimal points sample code_javascript skills
Question: Multiple ways to format data in JS with functions retaining two decimal places
The best way:
It seems like this is how we keep two people
alert(Number.toFixed(9.39393));
The returned value is 9.39
But only versions above ie5.5 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 2:
<script> tmp = "1234567.57232" result = tmp.substr(0,tmp.indexOf(".")+3); alert(result); </script>
Method 3: