在javascript中进行浮点比较

时间:2012-09-03 13:18:31

标签: javascript floating-point compare

这怎么可能100> = 99.2是假的?

var ls = parseFloat(("100").replace(",", ".")).toFixed(1);
var val = parseFloat(("99,2").replace(",", ".")).toFixed(1);
alert(ls >= val); /*=> result is false  ...but it should be true */

ui culture is nl-BE

jsfiddle:http://jsfiddle.net/Ed6VY/

1 个答案:

答案 0 :(得分:5)

toFixed会产生一个字符串。字符串逐个字符进行比较。 "9"之后发现"1",因此"99.2"大于"100.0"