How to convert BigDecimal to a long type in Java?

To convert a BigDecimal to a long type, you can use the longValue() method. An example is shown below:

BigDecimal decimal = new BigDecimal("1234567890.123456789");
long result = decimal.longValue();
System.out.println(result);

Output:

1234567890

Note that if a BigDecimal value exceeds the range of a long type, it will throw an ArithmeticException. Therefore, it is advisable to use the compareTo() method to check if the BigDecimal value is within the range of a long type before conversion.

bannerAds