0

Something that's always puzzled me is comparing floating point values in JS with zeros at end.

A simple use case would be comparing version numbers.

I want to check if the current version is less than something like 6.10.

But when doing below, it doesn't work because parseFloat removes the zero from 6.10, effectively making it 6.1 and invalidating the comparison.

var currentVersion = '6.10'
if(parseFloat(currentVersion) > parseFloat('6.9')) {
  // do something
}

The only other way, sans node-module, I've seen it done is to split the values on ".", and compare them individually.

Would love an easier solution that would work in the browser.

EDIT As pointed out, I was thinking of version numbers as floating point values when I shouldn't have. Thanks for all the answers.

vernak2539
  • 574
  • 3
  • 14

0 Answers0