0

When I divided the large number into a small number then the division is correct but when I write the small number to divide a large number the answer returns wrong. In my scenario, the small number always be first. here is my code this code return 7.4074074074074075E-6 but the correct result is 0.0000074074.

    double itf = 0.0;
    double a = 4.0;
    double b = 540000;
    itf = a / b;
    Log.i(TAG, "savedata: outputvalue=" + itf);
Muhammad Asad
  • 694
  • 6
  • 10
  • 3
    Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Markus Kauppinen Mar 12 '21 at 12:55

1 Answers1

2
BigDecimal a = new BigDecimal("4");
BigDecimal b = new BigDecimal("540000");

// 0.0000074074
a.divide(b, MathContext.DECIMAL128);

You should use a decimal type. double is outside the scope of support

Ashton Yoon
  • 172
  • 2
  • 6