How to compare decimal numbers with JavaScript?

Sometimes, we want to compare decimal numbers with JavaScript.

In this article, we’ll look at how to compare decimal numbers with JavaScript.

How to compare decimal numbers with JavaScript?

To compare decimal numbers with JavaScript, we can round both numbers and then compare them.

For instance, we write:

const a = 99.99
const b = 101.99
console.log((Math.round(a * 100) > Math.round(b * 100)))

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.

Conclusion

To compare decimal numbers with JavaScript, we can round both numbers and then compare them.