Solved

BigDecimal SetScale()  How?

Posted on 2000-04-13
4
2,980 Views
Last Modified: 2012-06-27
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
Comment
Question by:gbzhhu
  • 3
4 Comments
 
LVL 4

Expert Comment

by:jerch
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

by:
jerch earned 100 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

by:gbzhhu
ID: 2718000
Superp answer jerch

Thanks
0
 
LVL 4

Expert Comment

by:jerch
ID: 2718155
you're welcome :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now