Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

zero suppression

 
murat gungor
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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.

 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • 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...
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
That was the first thing I did...
 
Dave Turner
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic