admin管理员组文章数量:1415444
Why is "greater than" parisons for number values in JavaScript not working. The example below keeps returning true even when the mini number is less than the maxi.
mini and maxi are form input values. This example is using jQuery to get the values, but could easily be stripped.
var mini = $('form#filterPrice input.min').val(); //eg. 500
var maxi = $('form#filterPrice input.max').val(); //eg. 1500
if( mini.valueOf() > maxi.valueOf() ) { //also used: mini > maxi
alert('test'); //alerts "test" even when mini is less than maxi
$('form#filterPrice input.min').val( maxi ); //should switch values if mini > maxi
$('form#filterPrice input.max').val( mini );
}
Replacing "mini > maxi" with "Math.max(mini, maxi) == mini" works fine. So, the following does work:
var mini = $('form#filterPrice input.min').val(); //eg. 500
var maxi = $('form#filterPrice input.max').val(); //eg. 1500
if( Math.max(mini, maxi) == mini ) {
alert('test');
$('form#filterPrice input.min').val( maxi );
$('form#filterPrice input.max').val( mini );
}
Why is "greater than" parisons for number values in JavaScript not working. The example below keeps returning true even when the mini number is less than the maxi.
mini and maxi are form input values. This example is using jQuery to get the values, but could easily be stripped.
var mini = $('form#filterPrice input.min').val(); //eg. 500
var maxi = $('form#filterPrice input.max').val(); //eg. 1500
if( mini.valueOf() > maxi.valueOf() ) { //also used: mini > maxi
alert('test'); //alerts "test" even when mini is less than maxi
$('form#filterPrice input.min').val( maxi ); //should switch values if mini > maxi
$('form#filterPrice input.max').val( mini );
}
Replacing "mini > maxi" with "Math.max(mini, maxi) == mini" works fine. So, the following does work:
var mini = $('form#filterPrice input.min').val(); //eg. 500
var maxi = $('form#filterPrice input.max').val(); //eg. 1500
if( Math.max(mini, maxi) == mini ) {
alert('test');
$('form#filterPrice input.min').val( maxi );
$('form#filterPrice input.max').val( mini );
}
Share
Improve this question
asked Aug 12, 2009 at 14:55
AndresAndres
5,2286 gold badges32 silver badges35 bronze badges
1
- 1 Javascript may be treating mini and maxi as strings, and just lexically paring them. In which case '500' is greater than '1500'. Try converting mini and maxi to integers before paring them. – Karl Commented Aug 12, 2009 at 14:58
2 Answers
Reset to default 4Use this line to get Int from String
if( parseInt(mini.valueOf(),10) < parseInt(maxi.valueOf(),10) ) { //also used: mini > maxi
sinve you get values to pare from dom like a string. Fist you should parse them into Int and only then pare. Also it is better to put 10 as a second param to be sure the number will be parsed as decimal.
in first example you are paring strings and "1500" < "500"
. In second example Math.max
converts the values to numbers.
本文标签: mathJavaScript not comparing minimum greater than maximum number valuesStack Overflow
版权声明:本文标题:math - JavaScript not comparing minimum greater than maximum number values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745171067a2645976.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论