Is there a way to compare two numbers of the type “0.9.3” and “0.10.0” and get the right result? I am using localeCompare but I do not get it right, I also tried some arguments like {numeric: “true”}.
Many thanks!
Not entirely sure I understand what you are trying to do. You can program something like the snippet below:
if(x == "0.9.3")
{
console.log("The version is 0.9.3");
}
else if(x == "0.10.0")
{
console.log("The version is 0.10.0");
}
Bill, sorry for my explanation… I am trying to see if the firmware version of the devices has an update, and if it does, show ann alert. For this I need to compare, for example, the current version “0.9.3” witrh the new one “0.10.0” and I tried the “localeCompare”:
fw_id.localeCompare(beta_update);
But since 1 < 9 it gives the wrong answer. I also tried with the parameter {numeric: “true”}:
fw_id.localeCompare(beta_update, ‘en-US’, {numeric: “true”});
But it does not work either…
What will be the right function to compare these two strings of numbers?
Many thanks Bill !
You can convert those strings to an array with the “split” method & compare the numbers that way.
var version1 = v1str.split('.');
var version2 = v2str.split('.');
var version1Minor = version1[1];
var version2Minor = version2[1];
if(version1Minor < version2Minor )
{
console.log("do something");
}
Bill, ok many thanks!
Bill, actually it was a little bit more complicated than what I expected because I had to check the 3 parts of the firmware version plus the 4th part since you can get a firmware of the like “0.10.0-beta1” and thus you could get a “0.10.0-beta2”. On top of this each of the numeric parts need to be converted with “parseInt()” so as the comparison works! Quite a cumbersome process… But it finally worked…
Thanks!