• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rounding BigDecimal Number

 
Skanda Raman
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.

>
 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • 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".
 
Wouter Oet
Saloon Keeper
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
Sheriff
Posts: 48381
56
  • 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