• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Decimal Format

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic