0

Can someone explain to me why the following sum gives wrong result in dart?

final double result = 90071992547409.9 + 0.01;
print(result);

It prints the number 90071992547409.92

Mark Watney
  • 307
  • 5
  • 13

1 Answers1

0

According to Dart documentation:

Dart doubles are 64-bit floating-point numbers as specified in the IEEE 754 standard.

It's because of floating-point arithmetic. In your case (I used this converter):

90071992547409.9 = 90071992547409.90625 ~= 90071992547409.91

0.01 = 0.01000000000000000020816681711721685132943093776702880859375 ~= 0.01

90071992547409.91 + 0.01 = 90071992547409.92

The best solution in dart is to use the decimal package.

Owczar
  • 2,335
  • 1
  • 17
  • 24