*
The moose likes Beginning Java and the fly likes Rounding BigDecimal Number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Rounding BigDecimal Number" Watch "Rounding BigDecimal Number" New topic
Author

Rounding BigDecimal Number

Eshwar Prasad
Ranch Hand

Joined: Mar 21, 2008
Posts: 202
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

Joined: Oct 13, 2005
Posts: 38007
    
  22
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

Joined: Oct 25, 2008
Posts: 2700

BigDecimals are immutable thus the first method call in breakUpIncome is unnecessary.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
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.
 
jQuery in Action, 2nd edition
 
subject: Rounding BigDecimal Number
 
Similar Threads
Rounding BigDecimal Calculation Yielded Wrong Result
ejb hanging
multithreading not working correctly
Returning Multiple Variables
How to run the ConverterClient app?