aspose file tools*
The moose likes Beginning Java and the fly likes Double.parseDouble(String s) not handling decimal as expected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Double.parseDouble(String s) not handling decimal as expected" Watch "Double.parseDouble(String s) not handling decimal as expected" New topic
Author

Double.parseDouble(String s) not handling decimal as expected

Brady Diggs
Ranch Hand

Joined: Jun 26, 2008
Posts: 42
Hi all -
Not sure what is happening here, so I thought I'd check with all you experts. :-)

Here is my code:


The variable is "0.001" but comes out as 0.0010

I don't want the trailing zero. Where is is coming from?

Thanks in advance for any response or consideration.

Brady
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18966
    
  40

I don't want the trailing zero. Where is is coming from?


Floating point numbers (either float or double) don't have any formatting -- it is just a value.

Question: You've gone through much trouble to format the number, as a string. Why would you convert it back to a floating point before you print it? Isn't it better to just print the string?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Brady Diggs
Ranch Hand

Joined: Jun 26, 2008
Posts: 42
Thanks for your response, Henry.
Problem is I have to show it in a table (String)
and also pass it to a mainframe that requires a double.

I looked into floating-point arithmetic and have discovered
that there are many caveats to dealing with double values.
See this link:

http://docs.sun.com/source/806-3568/ncg_goldberg.html

Thanks,
Brady
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18966
    
  40

Thanks for your response, Henry.
Problem is I have to show it in a table (String)
and also pass it to a mainframe that requires a double.


That's fine. Then use the string to print and use the double for the mainframe. But don't expect the mainframe to format it -- as double doesn't have any formatting.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Double.parseDouble(String s) not handling decimal as expected