2

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?

Mardymar
  • 419
  • 3
  • 14
  • https://stackoverflow.com/questions/7641818/how-can-i-remove-the-decimal-part-from-javascript-number – Hamboy75 Jun 15 '17 at 19:59
  • 1
    https://stackoverflow.com/questions/15762768/javascript-math-round-to-two-decimal-places – Spiff Jun 15 '17 at 20:00
  • Can you clarify: Do you want to change the values in the variables, or is it about the number of digits to show when printing? If it is about the values themselves, remember that not all numbers can be represented as floating-point values, so some error at the end of the decimals is to be expected. – Thomas Padron-McCarthy Jun 15 '17 at 20:00
  • I don't see how your numbers have different levels of precision, we can't tell that from looking at the value. Did you determine the precision of your calculations or measurements? – Bergi Jun 15 '17 at 20:03
  • 2
    Possible duplicate of [How to deal with floating point number precision in JavaScript?](https://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript) – JJJ Jun 15 '17 at 20:04
  • I mean that I want the last number and all the zeros removed. – Mardymar Jun 15 '17 at 20:05

3 Answers3

0

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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

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);
Dalin Huang
  • 11,212
  • 5
  • 32
  • 49
-1

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));
Liz Turi
  • 51
  • 1
  • 1
  • 9
  • Code blocks on their own are not usually useful answers, and are more likely to attract downvotes. Please *explain* what the solution you're showing does, and *why/how* that code answers the question. – Heretic Monkey Jun 15 '17 at 20:15
  • The OP is really looking for a way to go around the floating point imprecision. There might be more than one "extra" digit at the end, and it might be a string of 9s instead of 0s. Also, this function breaks numbers that *don't* have the trailing zeros + end digit. – JJJ Jun 17 '17 at 07:46
  • Per the OP's comment, they were looking to remove the last digit and the zeros. I understand what you're saying, but I wasn't looking to provide an all purpose solution, but a specific solution to the specific question and parameters that the OP pointed out: "2 Possible duplicate of How to deal with floating point number precision in JavaScript? – JJJ Jun 15 at 20:04 I mean that I want the last number and all the zeros removed. – Mardymar Jun 15 at 20:05" – Liz Turi Jun 22 '17 at 16:32