Javascript floating point comparison

how is it possible that 100> = 99.2 is false?

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 - nl-BE

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

+4
source share
1 answer

toFixed results in a string. Lines are compared by character. "9" appears after "1" , so "99.2" greater than "100.0" .

+5
source

Source: https://habr.com/ru/post/1432090/


All Articles