简体   繁体   English

比较数字-异常行为

[英]Comparing numbers - Unusual behaviour

Im getting 2 values from a table and I'd like to compare them. 我从一个表中获得2个值,我想比较它们。 If one is grater than the other, then I alert some error. 如果一个比另一个更好,那么我会警告一些错误。 The thing is im getting a wrong result... for example I have 1000.00 and 1000000.00 and when I do 1000.00 > 1000000.00 Im getting a TRUE!, therefore an error, Why is that happening? 问题是我得到了错误的结果...例如,我有1000.00和1000000.00,当我执行1000.00> 1000000.00我得到TRUE !,因此出现错误,为什么会这样?

Hope someone can help me out here! 希望有人可以在这里帮助我! Thx 谢谢

row = document.getElementById(tabla).rows;
cell = document.getElementById(tabla).rows[0].cells;

for (i=2;i<row.length-1;i++){
    var TotalDist = parseFloat(document.getElementById(tabla).rows[i].cells[cell.length].firstChild.nodeValue).toFixed(2);
    var Total = document.getElementById(tabla).rows[i].cells[cell.length+1].firstChild.nodeValue;

    if(TotalDist>Total)
       alert("Error");
}

One of the values is already a float that I get from a db, the other one (TotalDist) is set by the user, so I convert it to float so that they can be compared properly... but it doesnt seem to be working 其中一个值已经是我从数据库中获取的浮点数,另一个值(TotalDist)是由用户设置的,因此我将其转换为浮点数,以便可以对其进行正确比较...但是它似乎不起作用

You need to convert both values to a number (ie use parseFloat on both strings) -> 您需要将两个值都转换为数字(即在两个字符串上都使用parseFloat )->

row = document.getElementById(tabla).rows;
cell = document.getElementById(tabla).rows[0].cells;

for (i=2;i<row.length-1;i++){
    var TotalDist = parseFloat(document.getElementById(tabla).rows[i].cells[cell.length].firstChild.nodeValue).toFixed(2);
    var Total = parseFloat(document.getElementById(tabla).rows[i].cells[cell.length+1].firstChild.nodeValue);

    if(TotalDist>Total)
       alert("Error");

}

When using nodeValue a string is returned ... see the docs 使用nodeValue返回一个string ... 请参阅文档

var value = node.nodeValue;

value is a string containing the value of the current node value是一个字符串,其中包含当前节点的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM