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

js format decimal

巴扎黑
巴扎黑Original
2016-11-25 14:56:011325browse

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>


Statement:
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