aspose file tools*
The moose likes Beginning Java and the fly likes Decimal Format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Decimal Format" Watch "Decimal Format" New topic
Author

Decimal Format

Stewart Johnson
Greenhorn

Joined: Dec 11, 2006
Posts: 5
Hi,

I am working on a problem where I have to format a number with two decimalplaces. Here is an 3example of how I am trying to do this:

NumberFormat formatter = new DecimalFormat("#.##");
String s = formatter.format(item.getPrice());
double testPrice = Double.parseDouble(s);
System.out.println(""+testPrice );

the problem is that when there are no decimal places in the price, the formatter only ads one zero whereas I need two decimal places like .00 to meet the busiiness requirement.

Thanks for your help.
[ December 11, 2006: Message edited by: Stewart Johnson ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
See what "0.00" does in the pattern. I always have to run a little trial and error to get these right.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Stewart Johnson
Greenhorn

Joined: Dec 11, 2006
Posts: 5
Thanks, that does work and gives me the correct output. Now the new problem is passing for example "3965.00" to the constructor of a BigDecimal

For example:

setNet_Price(new BigDecimal("3695.00"));

in this case, the .00 is removed

how can I stop the new BigDecimal("3695.00") from removing the zero's? it does not rmove decimal places othe than .00

Thanks!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14276
    
  21

You are confusing things. A BigDecimal only holds the value of the number; it does not hold a format. So if you do new BigDecimal("3695.00"), you get a BigDecimal object that represents the value 3695.

When you put the number on screen, then you use a DecimalFormat object to format it. The format is contained in the DecimalFormat object - not in the BigDecimal object.

I guess you have a class that contains a method setNet_Price(BigDecimal price) and a method BigDecimal getNet_Price(). You are calling getNet_Price() somewhere to get the price and put it on screen. In the code where you put it on screen, you need to use a DecimalFormat object with the right format again to format the number into a string, which you put on screen.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Stewart Johnson
Greenhorn

Joined: Dec 11, 2006
Posts: 5
Thanks, the place where the value comes back to the Screen is from a context defined BAPI node in SAP WebDynpro application. The table cells themselves are binded to the context nodes. Not sure how I will format the value there, and I suppose that is a SAP forum question. Thanks again for all the replies!
 
 
subject: Decimal Format