Say: var x = 6.450000000000003; var y = 5.234500000000002;
These are the results of floating point division, so the 3 and the 2 need to be removed. How can I trim x to 6.45 and y to 5.2345 given that they have different levels of precision?
Say: var x = 6.450000000000003; var y = 5.234500000000002;
These are the results of floating point division, so the 3 and the 2 need to be removed. How can I trim x to 6.45 and y to 5.2345 given that they have different levels of precision?
You could use Number#toFixed
and convert the string back to number.
var x = 6.450000000000003,
y = 5.234500000000002;
x = +x.toFixed(5);
y = +y.toFixed(5);
console.log(x);
console.log(y);
You could use Math.round
but you must choose a precision.
(Otherwise, you are losing percision and you don't want that!)
var x = 6.450000000000003;
var y = 5.234500000000002;
console.log(Math.round(x * 1000000) / 1000000);
console.log(Math.round(y * 1000000) / 1000000);
Try this function. If, as you say, you're simply looking to remove the end digit and remove trailing zeros, the following code could help.
function stripZeroes(x){
// remove the last digit, that you know isn't relevant to what
// you are working on
x = x.toString().substring(0,x.toString().length-1);
// parse the (now) String back to a float. This has the added
// effect of removing trailing zeroes.
return parseFloat(x);}
// set up vars for testing the above function
var x = 6.450000000000003;
var y = 5.234500000000002;
// test function and show output
console.log(stripZeroes(x));
console.log(stripZeroes(y));