Home > Web Front-end > JS Tutorial > js format decimal

js format decimal

巴扎黑
Release: 2016-11-25 14:56:01
Original
1324 people have browsed it

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:

<script> <p> tmp = "1234 567.57232" </p> result = tmp.substr( 0,tmp.indexOf(".")+3); <p> alert(result); <br> </script>


Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template