• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Brady Diggs
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21227
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Brady Diggs
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21227
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic