Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 17:07:03
Original
1446 people have browsed it
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.


Related labels:
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