Lucas Rodrigues
Lucas Rodrigues

Reputation: 49

Failed to multiply double in dart

I got an error when trying to multiply a number with a floating point in dart. Does anyone know why this happens?

void main() {
    double x = 37.8;
    int y = 100;

    var z = x * y;
    print(z);
    // 3779.9999999999995
}

In other languages ​​(C#/C++) I would have the result: 3780.0

Upvotes: 3

Views: 910

Answers (2)

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63749

You can use toStringAsFixed to control fraction digits.

void main() {
  double x = 37.8;
  int y = 100;

  var z = x * y;
  print(z.toStringAsFixed(1));
  // 3780.0
}

Upvotes: 2

Andy Brown
Andy Brown

Reputation: 13009

This is completely expected because 37.8 (or rather, the 0.8 part) cannot be precisely encoded as a binary fraction in the IEEE754 standard so instead you get a close approximation that will include an error term in the LSBs of the fraction.

If you need numbers that are lossless (e.g. if you are handling monetary calculations) then check out the decimal package.

A simpler hack if your floating point number has sufficient bits allocated to the fraction to keep the erroroneous bits out of the way is to round off the number after your calculation to the number of decimal places that you care about.

Upvotes: 2

Related Questions