How can i do arithmetic operations with large numbers in java? when i use double, it returns the result with E, can i avoid this way of representation? simple multiplication like 154.2 * 100000 giving result as 1.5419999999999998E7

Originally posted by Ulf Dittmer: Java has two classes for working with large integer and floating-point numbers. They're called java.math.BigInteger and java.math.BigDecimal, respectively.

Hi, thanks for the reply. I've already tried using BigDecimal, but got problems with precision

The problem is that you are constructing your BigDecimal from a "double". But the numbers you are trying to use cannot be perfectly represented by a double, so are wrong before ever becomeing a BigDecimal. To confirm this, print out the values of your BigDecimals, before the multiplication.

Instead, you should avoid floating-point (e.g. "double") altogether and construct your BigDecimal from a String or from a BigInteger. See the API for details of these constructors.

Any attempt to use floating-point arithmetic in combination with BigDecimal or BigInteger is likely to end in disappointment.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.

Devi Hari
Greenhorn

Joined: Oct 03, 2005
Posts: 7

posted

0

Originally posted by Peter Chase: The problem is that you are constructing your BigDecimal from a "double". But the numbers you are trying to use cannot be perfectly represented by a double, so are wrong before ever becomeing a BigDecimal. To confirm this, print out the values of your BigDecimals, before the multiplication.

Instead, you should avoid floating-point (e.g. "double") altogether and construct your BigDecimal from a String or from a BigInteger. See the API for details of these constructors.

Any attempt to use floating-point arithmetic in combination with BigDecimal or BigInteger is likely to end in disappointment.

That solved the problem.. Thanks a lot!!!

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.