admin管理员组文章数量:1432759
Lets say I have two vars called value1 and value2. These values would be input by the user but lets just pretend they did already and these are the values
var value1 = 5;
var value2 = 7;
var parison = .785
I just want to pare the values plus or minus the parison so when the code displays output it only displays the values of 5 + or - .785 and 7 + or minus .785
For some context the following code is a pulling information from a json object that has two separate lists of numbers lets say number1 and number2 and a ton of other info but dont worry about any of that, that all works fine
$("#createlist").click(function() {
var value1 = Number($("#value1").val());
var value2 = Number($("#value2").val());
var parison = .785
$.getJSON("get_divvy_data.php", null, function(data) {
var total_bikes_available = 0;
$("#stationtable .stationrow").remove();
$.each(data.stationBeanList, function(index, station) {
This next part is where I am having a problem with.
if( station.number1 is <= value1 + - parison && station.number2 is <= value2 + - parison) {
//do something
}
I just dont know how to write the if statement and parison effectively.
Lets say I have two vars called value1 and value2. These values would be input by the user but lets just pretend they did already and these are the values
var value1 = 5;
var value2 = 7;
var parison = .785
I just want to pare the values plus or minus the parison so when the code displays output it only displays the values of 5 + or - .785 and 7 + or minus .785
For some context the following code is a pulling information from a json object that has two separate lists of numbers lets say number1 and number2 and a ton of other info but dont worry about any of that, that all works fine
$("#createlist").click(function() {
var value1 = Number($("#value1").val());
var value2 = Number($("#value2").val());
var parison = .785
$.getJSON("get_divvy_data.php", null, function(data) {
var total_bikes_available = 0;
$("#stationtable .stationrow").remove();
$.each(data.stationBeanList, function(index, station) {
This next part is where I am having a problem with.
if( station.number1 is <= value1 + - parison && station.number2 is <= value2 + - parison) {
//do something
}
I just dont know how to write the if statement and parison effectively.
Share Improve this question edited Apr 9, 2014 at 22:59 nope asked Apr 9, 2014 at 21:46 nopenope 1771 gold badge4 silver badges16 bronze badges 1- just renamed some variables in last edit – nope Commented Apr 9, 2014 at 21:48
1 Answer
Reset to default 9One possible approach:
if (Math.abs(value1 - station.number1) <= parison
&& Math.abs(value2 - station.number2) <= parison) {
//...
}
... but be aware of possible edge cases caused by float-math imperfection. For example:
var value = 0.9;
var reference = 0.7;
var delta = 0.2;
console.log(value - reference <= delta); // false
本文标签: javascriptHow can I compare a number plus or minus another numberStack Overflow
版权声明:本文标题:javascript - How can I compare a number plus or minus another number - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745594666a2665403.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论