Two Laptop Bag*
The moose likes Java in General and the fly likes zero suppression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "zero suppression" Watch "zero suppression" New topic
Author

zero suppression

murat gungor
Greenhorn

Joined: Sep 12, 2001
Posts: 9
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.


murat
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
See the documentation for DecimalFormat and try a format string of "#######.0".


"I'm not back." - Bill Harding, Twister
murat gungor
Greenhorn

Joined: Sep 12, 2001
Posts: 9
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

Joined: Nov 06, 2000
Posts: 1510
As suggested by Jim, The API will show you what you need to do to get what you want. Try this "##########".


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
murat gungor
Greenhorn

Joined: Sep 12, 2001
Posts: 9
That was the first thing I did...
Dave Turner
Ranch Hand

Joined: Mar 13, 2001
Posts: 60
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

Joined: Sep 12, 2001
Posts: 9
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

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

Joined: Sep 12, 2001
Posts: 9
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: zero suppression