Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decimal comparison failing in JavaScript

I need to compare the following scenarios using decimal comparison in jquery.

var a=99999999999.99;
var b=9999999999999999999

if(parseFloat(a).toFixed(2) > parseFloat(b).toFixed(2))

This always returns true. How to fix the Issue?

Some of the outputs from what I tried:

parseFloat(9874563212).toFixed(2) > parseFloat(98745632).toFixed(2) true
parseFloat(98745632).toFixed(2) > parseFloat(987456321).toFixed(2) false
parseFloat(99999999999.99).toFixed(2) > parseFloat(9999999999999999999).toFixed(2) true
parseFloat(99999999999.99).toFixed(2) > parseFloat(999999999999).toFixed(2) false
parseFloat(99999999999.99).toFixed(2) > parseFloat(9999999999999).toFixed(2) false
parseFloat(99999999999.99).toFixed(2) > parseFloat(99999999999999).toFixed(2) false
parseFloat(99999999999.99).toFixed(2) > parseFloat(999999999999999).toFixed(2) false
parseFloat(99999999999.99).toFixed(2) > parseFloat(9999999999999999).toFixed(2) true
parseFloat(99999999999.99).toFixed(2) > parseFloat(1111111111111111).toFixed(2) true
parseFloat(99999999999.99).toFixed(2) > parseFloat(111111111111111).toFixed(2) true
like image 780
User Avatar asked Sep 11 '13 12:09

User


People also ask

How to compare decimals with JavaScript?

To compare decimal numbers with JavaScript, we can round both numbers and then compare them. to multiply a and b by 100 and call Math.round with the returned number to get rid of the decimals. Then we use > to compare them. Since a is smaller than b, the console log should log false.

Why can’t I use decimal numbers in JavaScript?

But why is that? In JavaScript all numbers are IEEE 754 floating point numbers. Due to the binary nature of their encoding, some decimal numbers cannot be represented with perfect accuracy. This is analagous to how the fraction 1/3 cannot be accurately represented with a decimal number with a finite number of digits.

How to cut decimals after 2 positions in JavaScript?

You decide to cut decimals after 2 positions, for example, and multiply the number by 100 to remove the decimal part. Use 10000 instead of 100 to keep 4 decimal positions. Download my free JavaScript Handbook!

What are the problems with decimal numbers in programming languages?

A better solution proposed by one of reader. Decimals will cause a big problem, especially the subtraction of decimal numbers. If you are not noticed a problem like this in live projects, this will create a big problem between the client and the software vendor. Many programming languages had these kinds of problems.


1 Answers

You are comparing strings, not numbers (.toFixed() returns a string). Try:

if (parseFloat(parseFloat(a).toFixed(2)) > parseFloat(parseFloat(b).toFixed(2)))

Or, if a and b are already numbers, as in your example

if (parseFloat(a.toFixed(2)) > parseFloat(b.toFixed(2)))

Or better

if (Math.round(a * 100) > Math.round(b * 100))
like image 200
xanatos Avatar answered Sep 24 '22 07:09

xanatos