I am building an app in Angular and I'm trying to find the most accurate way to perform simple mathematical equations. As a simple example, take the sum
0.1 + 0.2
As most of us know, this doesn't simply return 0.3, instead it returns
0.30000000000000004
However, in my app I require the results of these equations to be very accurate, i.e. I need 0.3 as the result. A common way to fix this is by using the toFixed
function:
(0.1 + 0.2).toFixed(2) = 0.30
However, this will not solve my issue. Let's say I need to divide 1 by 11, but in this case I actually need all decimal values:
1/11 = 0.09090909090909091
Whereas toFixed(2)
would actually return only 0.09
, but for my application I need higher precision, i.e. the result above.
Is there a way to perform these simple equations while still getting accurate results? i.e. 0.1 + 0.2 = 0.3
and 1/11 = 0.09090909090909091
.
EDIT
Here is another example:
15.7 + 17.4 + 17.39 = 50.48999999999999
whereas the answer should be 50.49