• Status: Solved
• Priority: Medium
• Security: Public
• Views: 3026

BigDecimal SetScale() How?

I am using BigDecimal format and when I say:

BigDecimal nCurrentNumber = new
BigDecimal ("9");
BigDecimal nTemp = new
BigDecimal ("5");

nCurrentNumber = nCurrentNumber.divide(nTemp, BigDecimal.ROUND_HALF_UP);

nCurrentNumber.setScale(28,BigDecimal.ROUND_UP);

I get a result of 2, but I am expecting
1.8  I doesn't actually give me any decimal point - why?  all operations return a number with no decimal places.

Can someone help me - urgent

Thanks
0
gbzhhu
• 3
1 Solution

Commented:
Hi gbzhhu...
Just change

nCurrentNumber.divide(nTemp, BigDecimal.ROUND_HALF_UP);

to
int scale = 2;
nCurrentNumber = nCurrentNumber.divide(nTemp, 2, BigDecimal.ROUND_UP);

This will produce 1.80.

The scale is the number of digits to the right of the decimal point. Of course it depends on what precision you want.

best regards...
Jerson

0

Commented:
By the way, same logic goes with the setScale() method...

e.g.
BigDecimal nCurrentNumber2  = new BigDecimal ("7.52434");
System.out.println(nCurrentNumber2.setScale(1, BigDecimal.ROUND_HALF_UP));

Take note that the setScale doesn't actually modify the value of the BigDecimal object you're calling from instead it returns a new BigDecimal.

Jerson
0

Author Commented:

Thanks
0

Commented:
you're welcome :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.