我应该如何在javascript或jquery中使用modulo? [英] How should I work with modulo in javascript or jquery?

查看:68
本文介绍了我应该如何在javascript或jquery中使用modulo?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是网络开发的新手,我的英语技能不足以在谷歌搜索该问题。我的问题是;

我正在验证双方的输入值。但是在前端,我在对数字进行验证之前将它们发送到后端。我有这种计算问题

1.25%0.05 = 0.0499999999999999993

这是错误的。你能解释一下我为什么以及如何做这种验证?



最好的问候



< b>我尝试了什么:



//我用estE = 1.25调用该函数

函数CheckValidNumber(estE) {

var m = .05;

var rest = estE%m;

alert(estE +%+ m += +休息);

if(rest === 0){

返回true;

}

否则{

返回false;

}



}

Hi I am newbie about web development and my English skills are not enough to search that problem in google. My problem is ;
I am validating input values in both sides.However In front-end side I am doing validation about numbers before send them to the back-end. I had this kind of calculation problem
1.25 % 0.05 = 0.04999999999999993
which is wrong. Could you explain me why and how should I do this kind of validations?

Best Regards

What I have tried:

// I call that function with estE = 1.25
function CheckValidNumber(estE) {
var m = .05;
var rest = estE % m;
alert(estE + "%" + m + " = " + rest);
if (rest === 0) {
return true;
}
else {
return false;
}

}

推荐答案

这不是一个完美的答案,但它有效。



This is not a perfect answer but it works.

var estE = 1.25;	   
var m = 0.05;
var rest =  parseInt(estE/m);
rest = parseInt(rest);

if((rest * m).toFixed(3) == estE.toFixed(3)){
	//alert(0); //same as mod===0
	return true;
}
else
{
	//alert(1) //same as mod===1
	return false;
}


这篇关于我应该如何在javascript或jquery中使用modulo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆