Here is an example of what Lukas Liesis suggested, note that this does work without the decimal spaces being used.
console.clear();
var min = parseFloat("5").toFixed(2);
var max = parseFloat("10").toFixed(2);
var value = parseFloat("7").toFixed(2);
console.log(min, max, value);
console.log(Math.round(min), Math.round(max), Math.round(value));
console.log(Math.round(value) > Math.round(min));
console.log(Math.round(value) < Math.round(max));
Now let's try to see how well it works with floating point precision.
console.clear();
var min = parseFloat("5.04").toFixed(2);
var max = parseFloat("5.06").toFixed(2);
var value = parseFloat("5.05").toFixed(2);
console.log(min, max, value);
console.log(Math.round(min), Math.round(max), Math.round(value));
console.log(Math.round(value) > Math.round(min));
console.log(Math.round(value) < Math.round(max));
As you can tell, it doesn't handle decimals very well.
Now finally, using the parseFloat method.
console.clear();
var min = parseFloat("5.04").toFixed(2);
var max = parseFloat("5.06").toFixed(2);
var value = parseFloat("5.05").toFixed(2);
console.log(min, max, value);
console.log(parseFloat(min), parseFloat(max), parseFloat(value));
console.log(parseFloat(value) > parseFloat(min));
console.log(parseFloat(value) < parseFloat(max));
This works with floating point precision as well, and if you werent going to use that precision, well, then you might as well just use integers instead, no reason to turn them into floats.
The reason why this works is that toFixed(2)
, makes JavaScript round the number to the second decimal place. Thus removing the noise.
This would also work for Lukas' example, but would still suggest reading Lukas' post as well since it does add context to why you should be a bit careful about using floats, since computers generally aren't the best at parsing them, as after the 15th significant digit it adds noise. (i.e. 1.99999999999999
instead of 2
)
TL;DR: If you don't care about the decimal precision, replace the parseFloat(5).toFixed(2)
with 5
, since after using Math.round that's what it's going to be anyways.
If you do care about decimal precision, use parseFloat()
, since toFixed()
turns it into a string, thus removing the ability to compare properly.
By the way, I know this is a late reply, but I wanted to add this context for people that also have a similar issue.
I also want to point out that you don't even need to use parseFloat()
when initializing the variables.
console.clear();
var min = (5.04).toFixed(2);
var max = (5.06).toFixed(2);
var value = (5.05).toFixed(2);
console.log(min, max, value);
console.log(parseFloat(min), parseFloat(max), parseFloat(value));
console.log(parseFloat(value) > parseFloat(min));
console.log(parseFloat(value) < parseFloat(max));
You might still want to surround the entire thing with parseFloat()
to avoid having to keep writing it though.
console.clear();
var min = parseFloat((5.04).toFixed(2));
var max = parseFloat((5.06).toFixed(2));
var value = parseFloat((5.05).toFixed(2));
console.log(min, max, value);
console.log(value > min);
console.log(value < max);
And yes, you do need the parentheses around the number to use toFixed()
.
var value = 1.toFixed(2);
console.log(value);