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 ;)

Google+

2 Comments

Rohit

about 3 years ago

Hi, 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

Reply

edwin

about 3 years ago

Hi Rohit, too bad i cant help you too much because i need to see whats your sourcecode is :(

Leave a Comment

Please be polite. We appreciate that.
Your email address will not be published and required fields are marked


:-[ (B) (^) (P) (@) (O) (D) :-S ;-( (C) (&) :-$ (E) (~) (K) (I) (L) (8) :-O (T) (G) (F) :-( (H) :-) (*) :-D (N) (Y) :-P (U) (W) ;-)