You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I tried to make a simple calculator using this library. But I am facing an issue with a very simple expression. I tried to multiple 2.3 with 3 and the answer received was 6.8999999999999995. Rather it should have been simply 6.9.
I have used the below code
I believe this is a similar issue as #27 and happens due to this library's use of 64 bit double-precision floating point numbers.
This is an issue that you will encounter in Dart and many other languages if you're not using big decimal representations.
I suggest to solve this you could round the results to the intended precision before presenting to the user/processing further.
This can be achieved by using Dart's toStringAsFixed method or writing your own rounding function.
For example:
voidmain() {
double result =2.3*3;
print(result); // prints 6.8999999999999995String resultRounded = result.toStringAsFixed(5);
print(resultRounded); // prints 6.90000double resultRoundedAsDouble =double.parse(resultRounded);
print(resultRoundedAsDouble); // prints 6.9
}
fkleon
added
question
Indicates that an issue or pull request needs more information
duplicate
Indicates similar issues or pull requests
labels
Apr 10, 2021
Hi, I tried to make a simple calculator using this library. But I am facing an issue with a very simple expression. I tried to multiple
2.3
with3
and the answer received was6.8999999999999995
. Rather it should have been simply6.9
.I have used the below code
The value of double returned here is 6.8999999999999995 instead of 6.9.
Another example is with the expression
2.1 * 3
which returned6.300000000000001
instead of6.3
.Same is the case with
52.2 % 2
which returned0.20000000000000284
instead of0.2
Can any one help me here?
The text was updated successfully, but these errors were encountered: