我从表格中获取了 2 个值,我想对它们进行比较。如果一个比另一个大,那么我会发出一些错误警报。 问题是我得到了错误的结果...例如,我有 1000.00 和 1000000.00,当我执行 1000.00 > 1000000.00 时,我得到一个 TRUE!,因此出现错误,为什么会发生这种情况?
希望有人能帮助我!谢谢
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");
}
其中一个值已经是我从数据库获取的 float ,另一个值(TotalDist)是由用户设置的,因此我将其转换为 float ,以便可以正确比较它们......但似乎并非如此正在工作
最佳答案
您需要将两个值转换为数字(即在两个字符串上使用 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");
}
当使用nodeValue
时,返回string
... see the docs
var value = node.nodeValue;
value is a string containing the value of the current node
关于javascript - 比较数字 - 异常行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11290494/