A Weird Exception When Using Java’s BigDecimal, java.lang.ArithmeticException: Rounding necessary
I found this weird exception when im trying to convert double values to BigDecimal,
java.lang.ArithmeticException: Rounding necessary at java.math.BigDecimal.divideAndRound(BigDecimal.java:1439) at java.math.BigDecimal.setScale(BigDecimal.java:2394) at java.math.BigDecimal.setScale(BigDecimal.java:2437)
This is my java code
BigDecimal bgAvg = new BigDecimal(0); bgAvg = bgAvg.add(new BigDecimal(myDoubleValue).setScale(2));
Somehow the error is gone after i changed my code into this
BigDecimal bgAvg = new BigDecimal(0); bgAvg = bgAvg.add(BigDecimal.valueOf(myDoubleValue).setScale(2));
Hope it helped others, have fun with Java
2 Comments
Rohit
about 6 years agoHi, I'm also encountering the java.lang.ArithmeticException: Rounding necessary in one of the webdynpro java applications. The full exception thread is as follows: java.lang.ArithmeticException: Rounding necessary atjava.math.BigDecimal.divideAndRound(BigDecimal.java:1439) at java.math.BigDecimal.setScale(BigDecimal.java:2390) at java.math.BigDecimal.setScale(BigDecimal.java:2323) at com.boots.devc.articlesearchdisplay.wd.comp.articlesearchcomp.ArticleSearchComp.setPurchasingTupleValues(ArticleSearchComp.java:10299) at com.boots.devc.articlesearchdisplay.wd.comp.articlesearchcomp.wdp.InternalArticleSearchComp.setPurchasingTupleValues(InternalArticleSearchComp.java:2363) ... 64 more Please let me know how this issue is resolved. Thanks, Rohit
Replyedwin
about 6 years agoHi Rohit, too bad i cant help you too much because i need to see whats your sourcecode is :(