Home >Web Front-end >JS Tutorial >js floating point numbers retain two decimal points sample code (rounding)_javascript skills

js floating point numbers retain two decimal points sample code (rounding)_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:07:031527browse
Copy code The code is as follows:

var changeTwoDecimal_f= function (floatvar){
var f_x = parseFloat (floatvar);
if (isNaN(f_x)){
return '0.00';
}
var f_x = Math.round(f_x*100)/100;
var s_x = f_x.toString();
var pos_decimal = s_x.indexOf('.');
if (pos_decimal < 0){
pos_decimal = s_x.length;
s_x = '.';
}
while (s_x.length <= pos_decimal 2){
s_x = '0';
}
return s_x;
}

Rounding function provided by js:
Copy code The code is as follows:

js rounding function toFixed( ), the parameter inside is the number of decimal places to retain.


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