javascript判断输入小数点后是否为两位的问题

来源:百度知道 编辑:UC知道 时间:2024/05/22 16:03:08
//判断限额只有小数点后两位by ben
function isTwoBitDecimal(d)
{
return (Math.round(d*100) == d*100);
}

这个函数输入
123.456
0.00001
1.001
都是成功的

但是输入
0.100
0.0100
等就判断失败了,为什么?应该怎么改进?
请问,小数点后不超过三位的的正则表达式怎么写?

Math.round()之后会求近似值。所以两个不相等
这个很简单吧?
function isTwoBitDecimal(d)
{
var temp = d+''; //int 转string
var stemp = temp.split('.');
if(stemp.length ==2 && stemp[1]==2){
return true;
}else{
return false;
}
}

用正则不是很简单吗?

var rex=/\d+[.]\d{2}/g

var s=输入的数字;

if(s.test(rex)) return ture;
else return false;