BigDecimal
You can use BigDecimal. BigDecimal represents an immutable arbitrary-precision signed decimal number. It consists of two parts:
Unscaled value – an arbitrary precision integer
Scale – a 32-bit integer representing the number of digits to the right of the decimal point
For example, the BigDecimal 3.14 has the unscaled value of 314 and the scale of 2.
We use BigDecimal for high-precision arithmetic. We also use it for calculations requiring control over scale and rounding off behavior. One such example is calculations involving financial transactions.
We can create a BigDecimal object from String, character array, int, long, and BigInteger:
BigDecimal number = new BigDecimal("45");
BigInteger
Or you can use BigInteger.
BigInteger number = new BigInteger("1234567890987654321");
BigInteger represents immutable arbitrary-precision integers. It is similar to the primitive integer types but allows arbitrary large values.
It is used when integers involved are larger than the limit of long type. For example, the factorial of 50 is 30414093201713378043612608166064768844377641568960512000000000000. This value is too big for an int or long data type to handle. It can only be stored in a BigInteger variable.
It is widely used in security and cryptography applications.