Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Big Decimal why use it?

Marco Vanoli
Ranch Hand
Posts: 99
Can somebody gimme some info about this Type (on java references i do not understand well what it is..)?

..or if you can give me just a link..

tnk bye

Nathaniel Stoddard
Ranch Hand
Posts: 1258
Well, as you know, doubles and floats are a specific number of bits wide. That means you can only cram so much information into them. 4.2 * 12.09 is no problem. But how about 34998729873948792879387948793879234.019872987349871234908798734928734928734987 * 0.00000000000000000000000000000023498729384729834792834792837492837492834729843273928?

Well, you get the idea! (Use BigDecimal.)

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
If you add, subtract, multiply, divide *money* BigDecimal will give you the answers you expect. Floats and Doubles will be full of unpleasant surprises.

marc weber
Sheriff
Posts: 11343

Marco Vanoli
Ranch Hand
Posts: 99
Originally posted by marc weber:

the result is :

0.30000000000000004
0.3
Press any key to continue...

ok and now con you quickly explain why, when use them?
and there is maybe a tutorial that explain use of Bigint?

marc weber
Sheriff
Posts: 11343
Originally posted by Marco Vanoli:
...con you quickly explain why, when use them? ...

Basically, if you're okay with small errors due to lack of precision -- like 0.30000000000000004 instead of 0.3 -- then doubles are fine. But whenever you need precise results, then use BigDecimal.
Originally posted by Marco Vanoli:
...there is maybe a tutorial that explain use of Bigint?

The API for BigInteger states...
BigInteger provides analogues to all of Java's primitive integer operators, and all relevant methods from java.lang.Math. Additionally, BigInteger provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.

[ July 23, 2005: Message edited by: marc weber ]

Marco Vanoli
Ranch Hand
Posts: 99
ok i found some intresting =)

class BigProblem2 {

public static void main(String[] args) {

// BigNums.java

System.out.println("Here's Long.MAX_VALUE: " + Long.MAX_VALUE);

BigInteger bInt = new BigInteger("3419229223372036854775807");

System.out.println("Here's a bigger number: " + bInt);

System.out.println("Here it is as a double: " + bInt.doubleValue( ));
}

}

import java.math.*;

Here's Long.MAX_VALUE: 9223372036854775807
Here's a bigger number: 3419229223372036854775807
Here it is as a double: 3.419229223372037E24
Press any key to continue...

[ July 23, 2005: Message edited by: Marco Vanoli ]

marc weber
Sheriff
Posts: 11343
Originally posted by Marco Vanoli:
...Here's Long.MAX_VALUE: 9223372036854775807
Here's a bigger number: 3419229223372036854775807
Here it is as a double: 3.419229223372037E24...

Yeah, I recall using BigInteger for handling very large numbers when I was writing the "ultimate" factoring program. (Doesn't everyone write one of these?) My program used BigInteger until the remainder was "small" enough to use primitives, when it switched over to long. I never actually verified whether this switch was more efficient.