JavaScript 判断一个数字是否含有小数点,如果含有,则返回该数字;如果不含小数点,则小数点后保留两位有效数字:
function hasDot(num){ if(!isNaN(num)){ return ( (num + '').indexOf('.') != -1 ) ? num: num.toFixed(2); } } var num = 16; console.log(hasDot(num)); // 16.00 var num2 = 18.01; console.log(hasDot(num2)); //18.01