This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Float.parseFloat(string) not accurate conversion

 
R Joshi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I understand that dealing with floats is not an accurate science .
But can I somehow specify that I need a correct precision upto 'n' digit after decimal point. How does 'rounding' work for floats? See the second example.


String s=5555.1234, Float.parseFloat(s)=5555.1235, Float.valueOf(s)=5555.1235 <===Note 5 at the end ?

String s=5555.6789, Float.parseFloat(s)=5555.6787, Float.valueOf(s)=5555.6787 <=== Note 7 at the end?

String s=5555.67898898098989797987987987987, Float.parseFloat(s)=5555.679, Float.valueOf(s)=5555.679 <== why only 3 digit at the end ?


I am using JDK1.5

RJ
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a reason you are using floats instead of doubles? Switching to doubles would fix this issue.

But it really is a weird one. I just tried parsing those strings to a double and the casting to a float, and the result was messed up again.



Yuriy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic