How to get biggest BigDecimal value

Viewed 104148

How can I get the largest possible value of a BigDecimal variable can hold? (Preferably programmatically, but hardcoding would be ok too)

EDIT
OK, just realized there is no such thing since BigDecimal is arbitrary precision. So I ended up with this, which is sufficiently big for my purpose:
BigDecimal my = BigDecimal.valueOf(Double.MAX_VALUE)

5 Answers

You can store Integer.MAX_VALUE + 1 integers in the magnitude, because arrays count from zero. All those integers got 32 bits, because they're all handled unsigned. So the precision in bits is 32*2147483648 = 68719476736 Bits (=8589934592Byte=8GiB=8,6GB). To get the precision in Decimals, you'll have to multiply the precision in Bits with log10(2) so you'll get 20686623783 full decimal digits, way over 4 times more, than a String can store. Now you can pow 10 with this amount of digits and subtract 1 to get the maximal BigDecimal value, but don't try to calculate it with BigDecimal itself. ;)

But now the question is... What comes first? The method .precision(), which is limited to Integer.MAX_VALUE or my calculated precision?

Related