Solved

BigDecimal SetScale()  How?

Posted on 2000-04-13
4
2,977 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

708 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

15 Experts available now in Live!

Get 1:1 Help Now