This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
when using "stripTrailingZeros()", it is strange but for instance for 0.5 value it displays 5E+1, for 0.1 it displays 1E+1.
What a pain!
I want this to be displayed as it is. I mean when I multiply a value with 100, I just want to get whatever the result is. setScale(2) is not working too, because if I had something like 0.0562 it throws exception, because the scale is forced to be 2.
aydeniz kirmizi wrote:setScale(2) is not working too, because if I had something like 0.0562 it throws exception, because the scale is forced to be 2.
The exception is thrown because you haven't told the BigDecimal how it should handle the excess values. You need to supply a rounding mode to the setScale method. The javadoc is quite clear about this.
Note that if you require a consistent number of decimal places, use setScale and not stripTrailingZeros, because it will pad or round to the desired number of places. If you don't want a fixed number of decimal places, then you shouldn't be using setScale at all.