7

I'm having slight difficulty in performing a calculation in Java. Here is what I'm trying to do -

((0.053800 * (500000/1000)) + 4) * 0.85

In my java application, it returns 26.264999999999997, which if you round up to 2 decimal places, becomes 26.26.

But in MS Excel, the same formula returns 26.265000.. and therefore the rounded result is 26.27.

If my Java application is returning incorrect value, what can I do to correct it?

12
  • 1
    The main difference is probably between floating-point arithmetic, and real math (as if done by hand). Commented Jun 3, 2011 at 2:25
  • 1
    Huh? It'll all be done in double-precision as is since floating-point literals without an "f" modifier are required to be double by the Java Language Spec, (and except for 500000/1000 which will be integer division (though it won't lose anything since there's no remainder to that quotient)). Commented Jun 3, 2011 at 2:29
  • 2
    Why did you round to 2 decimals? If you round to 3, they round to the same result. If you round to 1, they round to the same result. Commented Jun 3, 2011 at 2:29
  • 1
    Actually the Java app is returning the correct value, according to the IEEE 754 standard. Unfortunately, IEEE 754 arithmetic is different from real number arithmetic, and involves rounding operations on the numbers. Use BigDecimal if you want precise calculation of real numbers Commented Jun 3, 2011 at 2:36
  • 1
    @sean, I think QuantumMechanic was replying to pst Commented Jun 3, 2011 at 2:37

1 Answer 1

4

The following is a much better approximation, which results in the same value in this case:

import java.math.BigDecimal;
import java.math.MathContext;

public class Test {
    public static void main(String[] args) {
        //double d = ((0.053800 * (500000/1000)) + 4) * 0.85;
        BigDecimal d = ((new BigDecimal(0.053800).multiply(new BigDecimal(500000).divide(new BigDecimal(1000)))).add(new BigDecimal(4))).multiply(new BigDecimal(0.85));
        System.out.println(d.round(MathContext.DECIMAL32));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks everyone who suggested me to use BigDecimal. This does give the desired result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.