• 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

DecimalFormat dropping zeros

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic