# Decimal Format

Stewart Johnson
Greenhorn
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.

[ December 11, 2006: Message edited by: Stewart Johnson ]

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
See what "0.00" does in the pattern. I always have to run a little trial and error to get these right.

Stewart Johnson
Greenhorn
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
Posts: 15556
43
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.

Stewart Johnson
Greenhorn
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!