File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DecimalFormat dropping zeros

 
Jennifer Sohl
Ranch Hand
Posts: 455
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I am using a decimal formatter to format values to "0.00". The problem is, when I have a value that is 3.00, it shows up as 3. Same thing happens if it's 3.50, shows up as 3.5. What do I need to do to make it not drop the last zero?
Thanks!
 
Michael Morris
Ranch Hand
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to set the maximum and/or minimum integer and fraction digits like this:
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I tried your suggestion and it did not work for what I am doing.
I am placing a value into a table as a Double object. what I am doing is :


This did not change my value.
When I created the object into the table as a String using the formatter, it worked.
How can I format the string, and display it as a Double in the table with the trailing zero??
Thanks again!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Double (or double) does not know or care how many digits you want to display. Once its value is set, it doesn't matter whether you created it using 3.5 or 3.50 - it's exactly the same as far as Double and double are concerned. The way to control how it's displayed is to format as (just before) you display it), e.g.
System.out.println(df.format(rowData[x].doubleValue()));
Or you could save the String representation of the number, rather than the Double or double version. But for most applications that's less convenient.
Incidentally, your code can be streamlined in several ways:
rowData[x] = new Double(df.format(getCostNorm());
or
rowData[x] = new Double(getCostNorm());
The former is slightly different in that formatting and parsing the number has the effect of rounding off an info after the second digit - e.g. 3.504 becomes 3.5. But there's still no difference between 3.5 and 3.50, except when formatted as a String.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic