Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum number of digits after the decimal point using BigDecimal

What is the maximum number of digits we can have after the decimal point of a BigDecimal value in Java?

like image 915
Kaddy Avatar asked Oct 21 '10 18:10

Kaddy


1 Answers

It's (almost) unlimited. You can store roughly 2 billion digits after the decimal point if scale is set to the maximum value of an integer, although you may run out of memory if you try to do this. If you need to store so many digits that the limit is a problem then you probably need to rethink the design of your program.

See the BigDecimal documentation:

Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. The value of the number represented by the BigDecimal is therefore (unscaledValue × 10-scale).

like image 171
Mark Byers Avatar answered Sep 22 '22 10:09

Mark Byers