-5

I have made a Calculator app on android but whenever I do some multiplication of the kind:

23.3 x 3.3

The answer I get is something like:

76.988999999999

Now please tell me how to resolve this conflict with this app. The answer should be like

76.89
Himanshu Aggarwal
  • 1,803
  • 2
  • 24
  • 36

2 Answers2

6

Welcome to the world of IEEE-754. In this particular case, you can get a better result with BigDecimal rather than double:

import java.math.*;

public class BigCheck {
    public static final void main(String[] args) {
        BigDecimal a = new BigDecimal("23.3");
        BigDecimal b = new BigDecimal("3.3");
        System.out.println(a.multiply(b));
    }
}

...but BigDecimal has its own issues (for instance, try 10 / 3). But it's probably a better choice for a calculator app, just keep in mind that when dividing, you'll probably need to use the version that accepts a scale and rounding mode. And be sure to read the details of the class so you understand its concept of scale.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • it is not working for me. Say i am taking 2 double variables c & d and allowing the user to enter the values using Scanner class, then replacing "23.3" & "3.3" in your example with "c" & "d" resp. then the answer comes out to be: 76.88999999999999820587959220574690445766638900334550912342955475420325228697038 255631923675537109375 – Himanshu Aggarwal Mar 25 '13 at 09:05
  • Now tell me what should i use? – Himanshu Aggarwal Mar 25 '13 at 09:05
  • @HimanshuAggarwal: I *did* tell you: Use `BigDecimal`, not `double`. If you have `double` in there (anywhere), you will run into the inaccuracies inherent in IEEE-754 floating point. IEEE-754 is designed for speed and sacrifices some accuracy to achieve it. The classic example is `0.1 + 0.2`, which in IEEE-754 results in `0.30000000000000004`. `BigDecimal` is designed for accuracy when doing decimal calculations, at the expense of speed. – T.J. Crowder Mar 25 '13 at 09:07
  • i did explore this and found the way to using it. I actually wanted to take the input from the user so there comes that double, then making it a String, i can pass it it BigDecimal and the problem solved. thanks for the help – Himanshu Aggarwal Mar 25 '13 at 09:30
  • 1
    @HimanshuAggarwal: If you're taking the input from the user, why not just accept it as a string in the first place? Don't pass it through `double` first, that will introduce the inaccuracies. – T.J. Crowder Mar 25 '13 at 09:32
  • yep! you are right. I think my approach was wrong. thank you! :) – Himanshu Aggarwal Mar 25 '13 at 13:16
2

Your question is not clear. I guess you want to get the exact digits answer.

DecimalFormat df = new DecimalFormat(".##");
String result = df.format(76.988999999999);

EDIT

Example to learn this Decimal Format.

import java.text.DecimalFormat;

class Demo {
    public static void main(String args[])  
    {
        double b = 756.988999999999;
        DecimalFormat df = new DecimalFormat(".##");
        String result = df.format(b);
        System.out.printf(result);  
    }
}
Ajay S
  • 48,003
  • 27
  • 91
  • 111