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

DecimalFormat dropping zeros

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
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

Joined: Jan 30, 2002
Posts: 3451
You need to set the maximum and/or minimum integer and fraction digits like this:


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DecimalFormat dropping zeros