I'm facing the next problem. I am performing mathematical operations to calculate weights where I am using a function to return only 2 decimal places but sometimes the result is not as expected. Here is an example
I have a paper that weighs 49.8 and I want to subtract 1/4, that is 12.45 and do it 4 times but when I do this subtraction 49.8 - 12.45 the result gives me 37.34999999999999999999994 when in fact it should be 37.35 because if I do the whole process with my function the result would not be 0, it would be as follows
- 49.8 - 12.45 = 37.34
- 37.34 - 12.45 = 24.89
- 24.89 - 12.45 = 12.44
- And you can't 12.44 - 12.45
So that's the problem because 49.8/4 = 12.45 but the way I'm doing it is not possible
How can I solve this, a way where if the third decimal place of the number is 9 round it and return it with only 2 decimals otherwise use my function to return it with 2 decimals
My function:
function threeDecimals(num) {
const multipliedNum = num * 100;
const truncatedNum = Math.trunc(multipliedNum);
const num3decimals = truncatedNum / 100;
return num3decimals;
}
CodePudding user response:
If you need exact calculations, operate on smaller units (milligrams instead of grams), so that intermediate values are always integers:
weight = 49800
f = (weight / 4) | 0
weight -= f
weight -= f
weight -= f
result = weight / 1000
console.log(result)
CodePudding user response:
Floating point numbers have this property by design. If you need to have base-10 precision, it's not enough to add a few decimals and round, because errors can still slip in in higher precisions.
You will need to use a third party library that guarantees base10 accuracy for the examples you gave.
Note that nothing will give you accuracy for every type of operation. Even Decimal.js
will round 1/3
and not have a perfect representation, just like floating point numbers give seemingly 'incorrect' outcomes.
CodePudding user response:
let x = 49.8;
let y = 12.45;
let z = x-y;
let a = z.toFixed(2);
console.log('the value of z is: ' z);
console.log('the value of a is: ' a);
the output for the above code is in this link
Hope this code will be useful for you.