2

Possible Duplicate:
Java floats and doubles, how to avoid that 0.0 + 0.1 + … + 0.1 == 0.9000001?

How can I overcome the precision issue with double multiplication in java android??Please note that I am converting a string value into double value.

eg: when I multiply two double value:

double d1 = Double.valueOf("0.3").doubleValue() * Double.valueOf("3").doubleValue();
System.out.println("Result of multiplication : "+d1);

I am getting the following result : 0.8999999999999999

Some of the results that i am getting are.

0.6*3=1.7999999999999998;
0.2*0.2=0.04000000000000001;
etc.

Instead of the above results I would like to get the following results.

0.3*3=0.9;
0.6*3=1.8;
0.2*0.2=0.04;

Please remember that I am not trying to round it to the nearest integer.

Community
  • 1
  • 1
prajul
  • 1,216
  • 2
  • 15
  • 27

3 Answers3

11

You should really be using java.math.BigDecimal to avoid any precision issues, and always use a BigDecimal(String) constructor.

BigDecimal result = new BigDecimal("0.3").multiply( new BigDecimal("3.0") );
Strelok
  • 50,229
  • 9
  • 102
  • 115
  • 1
    I don't think it's accurate to say using BigDecimal will avoid *any* precision issues. At the end of day we have to make some sacrifices to represent as large a possible range of numbers in a finite amount of memory. – Adrian Mouat Nov 12 '11 at 11:29
  • 1
    @AdrianMouat, I am pretty sure if you use BigDecimal the way I specified (constructing it using `BigDecimal(String)` constructor), you will avoid any precision issues without sacrifices. – Strelok Nov 12 '11 at 11:37
  • 1
    In the given examples, sure. There still have to be limits however. – Adrian Mouat Nov 12 '11 at 12:25
  • 2
    @AdrianMouat the only limit is available memory. BigDecimal doesn't use floating point so it doesn't have this problem the way a double does. – user207421 Nov 12 '11 at 14:21
  • @EJP, that doesn't contradict what I said. It seems BigDecimal throws exceptions when you're number can't be represented in decimal (e.g. 1/3). So you are sacrificing flexibility for precision here I suppose. – Adrian Mouat Nov 13 '11 at 11:52
  • @AdrianMouat What does your last sentence mean? – user207421 Nov 13 '11 at 21:01
  • @EJP That BigDecimal forces you to consider your inputs and possible exceptions. In return you get a greater level of precision. – Adrian Mouat Nov 16 '11 at 12:20
  • you can use BigDecimal, but make sure that you use the construct of BigDecimal(String) rather than BigDecimal(double). see my post in https://stackoverflow.com/a/45292783/5113415 – learner Jul 25 '17 at 02:14
1

The problem isn't with multiplication. It starts with Double.valueOf("0.3"). That value can't be represented exactly in floating-point. You should use java.math.BigDecimal, and you should also Google for a page entitled "What every computer scientist should know about floating point".

user207421
  • 305,947
  • 44
  • 307
  • 483
0

Unfortunately, I am not aware of a simple way of doing exactly what you ask for.

Like Strelok says, you should not be using a floating-point type if you need exact results. However, for most purposes, it is enough to just specify a rounding precision for output. The following code is close to, but not quite, what you want:

System.out.printf("Result of multiplication : %.1g\n", d1);

For more info on the syntax of printf, see the java.util.Formatter documentation.

ibid
  • 3,891
  • 22
  • 17