js浮点数保留一位小数

原文链接: http://www.cnblogs.com/php-linux/p/4991874.html

function changeTwoDecimal_f(x) {
var f_x = parseFloat(x);
if (isNaN(f_x)) {
alert('function:changeTwoDecimal->parameter error');
return false;
}
var f_x = Math.round(x * 10) / 10;
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 + 1) {
s_x += '0';
}
return s_x;
}

转载于:https://www.cnblogs.com/php-linux/p/4991874.html

你可能感兴趣的:(js浮点数保留一位小数)