问题描述
当你改变数字的时候,它需要检查数值是否在数值字段中不仅在可接受的范围内,而且还在步骤中:
< input type =numbermin = 0max =100step =1/>
如果用户输入 5.5
截断这个最接近的步骤低于这个值,在这种情况下,这是 5
。
如果用户输入 我计划使用这个计算公式: 尽管出于测试目的,您可以简单地使用它: 这对于整数和更大的值很有用,但是我遇到了问题由于JavaScript是如何处理浮点数的,所以有小数。 例如: 每一行,在JavaScript中返回 可以做些什么来使这个函数更准确*? 您可以尝试确保 I am creating number spinner widget in JavaScript to essentially mimic the number field in webkit. When you change the number, it needs to check to see if the value is not only within the accepted range, but also that it's in step: If a user enters For a step of The equation I was planning on using for this calculation looks like this: Although for testing purposes, you could simply use this: This works great for integers and larger values, however I've run into issues with decimals due to how JavaScript handles floating point numbers. For example: In analyzing each line, it turns out that What can be done to make this function more accurate*? *accurate being that it works for increments of You could try making sure step is greater than 1 (by repeatedly multiplying by 10), then do your modulus, then scale back down to original. For example: This works for the examples you provided, but I can't guarantee it will work for everything. See the jsfiddle here: 这篇关于JavaScript中的精确浮点运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 5.5
,结果将是 4 $ c $
... code ...
_checkStep:function(val){
val ret,
diff,
moddedDiff;
diff = val - this._min;
moddedDiff = diff%this._step;
ret = val - moddedDiff;
return ret;
},
//在别处设置
_min,
_step,
...更多代码...
function checkStep(val,min,step){
var ret,
diff,
moddedDiff;
diff = val - min;
moddedDiff = diff%step;
ret = val - moddedDiff;
return ret;
$ b $ p
$ b
checkStep(0.5,0,0.1)//返回0.4,预计为0.5
0.5%0.1
返回 0.09999999999999998
。
0.01
以上的增量。 步骤大于1(通过反复乘以10),然后做你的模数,然后缩小到原来的。例如:
函数checkStep(val,min,step){
var ret,
diff,
moddedDiff;
var pows = 0;
(步骤< 1){//确保步骤> 1
step * = 10;
val * = 10;
min * = 10;
pows ++;
}
diff = val - min;
moddedDiff = diff%step;
ret = val - moddedDiff;
return ret / Math.pow(10,pows);
$ / code $
$ b $ p这适用于你提供的例子,但我不能保证会为一切工作。在这里看到jsfiddle:
<input type="number" min="0" max="100" step="1" />
5.5
the field will truncate this to the closest step lower than the value, which in this case is 5
.2
, if the user entered 5.5
, the result would be 4
....code...
_checkStep: function (val) {
val ret,
diff,
moddedDiff;
diff = val - this._min;
moddedDiff = diff % this._step;
ret = val - moddedDiff;
return ret;
},
//set elsewhere
_min,
_step,
...more code...
function checkStep(val, min, step) {
var ret,
diff,
moddedDiff;
diff = val - min;
moddedDiff = diff % step;
ret = val - moddedDiff;
return ret;
}
checkStep(0.5, 0, 0.1) //returns 0.4, 0.5 is expected
0.5 % 0.1
in JavaScript returns 0.09999999999999998
.0.01
and up.function checkStep(val, min, step) {
var ret,
diff,
moddedDiff;
var pows = 0;
while( step < 1 ) { // make sure step is > 1
step *= 10;
val *= 10;
min *= 10;
pows++;
}
diff = val - min;
moddedDiff = diff % step;
ret = val - moddedDiff;
return ret / Math.pow( 10, pows );
}