• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

zero suppression

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I obtain an empty string for a double that has the value 0.0? I used the code below to achieve this, but I still get "0"...
double numberVal = Double.parseDouble(stringValue);
String formattedValue = new DecimalFormat("#######").format(numberVal));
Thanks in advance.

 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the documentation for DecimalFormat and try a format string of "#######.0".
 
murat gungor
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
See the documentation for DecimalFormat and try a format string of "#######.0".


This pattern generates ".0" instead blanks...
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As suggested by Jim, The API will show you what you need to do to get what you want. Try this "##########".
 
murat gungor
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was the first thing I did...
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try inserting an if statement to check the value of the double before formating it to a string. Then you only need format it when it is greater than 0.0d (or less than 0.0d too).

Hope this helps
Dave
 
murat gungor
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dave. I have no doubt that this brute force method would work. I was hoping that someone would admit DecimalFormat("#######") is not doing what it promisses to do...
p.s. The problem with this method is, I have to check the pattern too, to see if it is all '#'s, which means I am implementing a kind of extension to format method of DecimalFormat...
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sigh. Read the documentation. "#######" does exactly what it says it will do. So does "#######.0" - I just overlooked your need for a leading zero in the ones position. So use "#######0.0". Test it for other numbers too, to verify it does what you want.
 
murat gungor
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
Sigh. Read the documentation. "#######" does exactly what it says it will do. So does "#######.0" - I just overlooked your need for a leading zero in the ones position. So use "#######0.0". Test it for other numbers too, to verify it does what you want.


I appreciate your help. I guess my wording is not as exact as you expect. I need nothing but ""(blank or zero length string) if the number is 0(zero)... The pattern ###### is supposed to do that. I did read the doc and that's where I got it from. My problem is, it's not doing what it says it will do. I tested it. It is not doing it. Maybe I should blame the jvm not the language.
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic