Solved

BigDecimal SetScale()  How?

Posted on 2000-04-13
4
2,986 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
[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
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

733 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