Converting BigDecimal to Integer

Vishal picture Vishal · Oct 28, 2010 · Viewed 305.1k times · Source

I have Hibernate method which returns me a BigDecimal. I have another API method to which I need to pass that number but it accepts Integer as parameter. I cannot change return types or variable types of both methods.

Now how to convert the BigDecimal into Integer and pass it to second method?

Is there a way out of this?

Answer

willcodejavaforfood picture willcodejavaforfood · Oct 28, 2010

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.