四舍五入保留2位小数(不够位数,则用0替补)
function keepTwoDecimalFull(num) {var result = parseFloat(num);if (isNaN(result)) {alert('传递参数错误,请检查!');return false;}result = Math.round(num * 100) / 100;var s_x = result.toString(); //将数字转换为字符串var pos_decimal = s_x.indexOf('.'); //小数点的索引值// 当整数时,pos_decimal=-1 自动补0if (pos_decimal < 0) {pos_decimal = s_x.length;s_x += '.';}// 当数字的长度< 小数点索引+2时,补0while (s_x.length <= pos_decimal + 2) {s_x += '0';}return s_x;
}console.log(keepTwoDecimalFull(120.5)); //120.50console.log(typeof keepTwoDecimalFull(120.5)); //stringconsole.log(keepTwoDecimalFull(2.446242342)); //2.45console.log(typeof keepTwoDecimalFull(2.446242342)); //string
感谢您的支持
