This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Rounding BigDecimal Number  RSS feed

 
Ranch Hand
Posts: 205
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a requirement where the input number is 30.5 and break up scale is 10. so the out put should be as 10+10+10+0.5

Here,

a) In line 28, I am trying to add the values in variable totalIncome. But it is not getting populated.
b) Each time while rounding the number, I am dividing it with 1 and setting the scale as 2. Is there any other way to do this.
c) Is there any other best way to write this program for the given requirement.

Please give your suggestions and let me know.

Code below.

>
 
Marshal
Posts: 58421
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really are making things difficult for yourself. Read the API documentation for BigDecimal and you find it mas methods for rounding, and constant ONE which you could use. you should never pass a fractional double to the BigDecimal constructor, because the imprecision in 888888.88 will be perpetuated and immortalised; you should use Strings eg "888888.88".
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BigDecimals are immutable thus the first method call in breakUpIncome is unnecessary.
 
Campbell Ritchie
Marshal
Posts: 58421
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Wouter Oet wrote:BigDecimals are immutable thus the first method call in breakUpIncome is unnecessary.

. . . and, not being on the right of a = sign, ineffectual.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!