javaintegercastingbigdecimal

Converting BigDecimal to Integer


I have a Hibernate method which returns a BigDecimal.

I have another API method to which I need to pass that number but it accepts Integer as the parameter. I cannot change return types or variable types of either method.

How to convert the BigDecimal into an Integer and pass it to the second method?


Solution

  • You would call myBigDecimal.intValueExact() (or just intValue()) and it will even throw an exception if you would lose information. That returns an int but autoboxing takes care of that.