简体   繁体   中英

Dart double division precision

What is the correct way to perform this operation?

399.9 / 100

What I would expect to see is

3.999

but the result is

3.9989999999999997

The result you see is correct , it's just not what you want.

Doubles are not precise values. The double you get by writing 399.9 is actually the precise value.

399.8999999999999772626324556767940521240234375

That's the closest available double to the exact value 399.9. Any other double is at least as far away from 399.9 as that.

Then you divide by 100. Again, the result is not precise, but the closest double has the exact value

3.99899999999999966604491419275291264057159423828125

That differs from what you would get by writing 3.999, which is the exact value:

3.999000000000000110134124042815528810024261474609375

At every step, the double operations have minimized the error, but because you are doing multiple steps, the final result diverges from the double closest to the mathematical result.

What you need to do depends on what your actual requirements are.

If you want to always calculate with two significant digits, then I'd just multiply my numbers with 100 and do all the operations as integer operations, until the very last division by 100.

If you have an intermediate result and wants to round it to two digits, I'd do what Fy Z1K says:

  result = (result * 100).round() / 100;
import 'dart:math';

double roundDouble(double value, int places){ 
    double mod = pow(10.0, places); 
    return ((value * mod).round().toDouble() / mod); 
}

then you would basically get

double num1 = roundDouble(12.3412, 2);
// 12.34

double num2 = roundDouble(12.5668, 2);
// 12.57

double num3 = roundDouble(-12.3412, 2);
// -12.34

double num4 = roundDouble(-12.3456, 2);
// -12.35

To make decimal operations you can use the decimal package .

final d = Decimal.parse;
print(d('399.9') / d('100')); // => 3.999

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM