Javascript: Comparing two float values

52,593

Solution 1

toFixed returns a string, and you are comparing the two resulting strings. Lexically, the 1 in 12 comes before the 7 so 12 < 7.

I guess you want to compare something like:

(Math.round(parseFloat(acVal)*100)/100)

which rounds to two decimals

Solution 2

Compare float numbers with precision:

var precision = 0.001;

if (Math.abs(n1 - n2) <= precision) {
  // equal
}
else {
  // not equal
}

UPD: Or, if one of the numbers is precise, compare precision with the relative error

var absoluteError = (Math.abs(nApprox - nExact)),
  relativeError = absoluteError / nExact;

return (relativeError <= precision);

Solution 3

The Math.fround() function returns the nearest 32-bit single precision float representation of a Number.

And therefore is one of the best choices to compare 2 floats.

if (Math.fround(1.5) < Math.fround(1.6)) {
    console.log('yes')
} else {
    console.log('no')
}

>>> yes

// More examples:
console.log(Math.fround(0.9) < Math.fround(1));                            >>> true
console.log(Math.fround(1.5) < Math.fround(1.6));                          >>> true
console.log(Math.fround(0.005) < Math.fround(0.00006));                    >>> false
console.log(Math.fround(0.00000000009) < Math.fround(0.0000000000000009)); >>> false

Solution 4

Comparing floats using short notation, also accepts floats as strings and integers:

var floatOne = 2, floatTwo = '1.456';

Math.floor(floatOne*100) > Math.floor(floatTwo*100) 

(!) Note: Comparison happens using integers. What actually happens behind the scenes: 200 > 145

Extend 100 with zero's for more decimal precision. For example use 1000 for 3 decimals precision.

Test:

var floatOne = 2, floatTwo = '1.456';
console.log(Math.floor(floatOne*100), '>', Math.floor(floatTwo*100), '=', Math.floor(floatOne*100) > Math.floor(floatTwo*100));

Solution 5

Comparing of float values is tricky due to long "post dot" tail of the float value stored in the memory. The simplest (and in fact the best) way is: to multiply values, for reducing known amount of post dot digits to zero, and then round the value (to rid of the tail).

Obviously both compared values must be multiplied by the same rate.

F.i.: 1,234 * 1000 gives 1234 - which can be compared very easily. 5,67 can be multiplied by 100, as for reducing the float comparing problem in general, but then it couldn't be compared to the first value (1,234 vel 1234). So in this example it need to be multiplied by 1000.

Then the comparition code could look like (in meta code):

  var v1 = 1.234;
  var v2 = 5.67;

  if (Math.round(v1*1000) < Math.round(v2*1000)) ....
Share:
52,593
Harold Sota
Author by

Harold Sota

I have ten years’ experience in software development and consultancy. Covering fully the software development lifecycle, worked in teams of various sizes and in the last four years had the opportunity to lead the team and successfully implemented numerous technologies in various projects.

Updated on August 14, 2021

Comments

  • Harold Sota
    Harold Sota over 2 years

    I have this JavaScript function:

    Contrl.prototype.EvaluateStatement = function(acVal, cfVal) {
    
        var cv = parseFloat(cfVal).toFixed(2);
        var av = parseFloat(acVal).toFixed(2);
    
       if( av < cv) // do some thing
    }
    

    When i compare float numbers av=7.00 and cv=12.00 the result of 7.00<12.00 is false!

    Any ideas why?