Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# BigDecimal SetScale()  How?

Posted on 2000-04-13
Medium Priority
3,006 Views
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
Question by:gbzhhu
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3

LVL 4

Expert Comment

ID: 2714922
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

LVL 4

Accepted Solution

jerch earned 400 total points
ID: 2714939
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

LVL 12

Author Comment

ID: 2718000

Thanks
0

LVL 4

Expert Comment

ID: 2718155
you're welcome :)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
###### Suggested Courses
Course of the Month9 days, 2 hours left to enroll