How BigDecimal is implemented?

Jan Ajan picture Jan Ajan · May 18, 2012 · Viewed 9.5k times · Source

I was reading BigDecimal Class but I was not able to find any information how BigDecimal class stores values in computer memory.

Do you know any reliable source that can provide this information?

Answer

Greg Lowe picture Greg Lowe · May 19, 2014

The unscaled value of the BigDecimal is stored in a BigInteger. The precision and scale are stored separately in integer fields:

BigInteger intVal
int scale
int precision

BigInteger stores the integer as a big-endian array of 32 bit integers, and the sign separately as another 32-bit integer.

int signum
int[] mag

But as Muhd says, if the number can fit in a 64 bit integer, then this is used instead of a BigInteger.