Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Conversion problem  RSS feed

 
Stephan Staeheli
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I read values from a file as Strings. One of this string value is a float and I want to convert this String to a Float. Now I do the following:



Very strange, but this returns a wrong value. What am I doing wrong? Is this some kind of rounding problem? How can I suppress rounding?

Regards,
Stephan
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like a rounding problem. float and double are NOT exact values. For the number of signigicant digits you have, there is probably some rounding.
Try using double. It can hold more significance.
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check if this link helps.
 
Stephan Staeheli
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, we are switching from float to double. And thank you for the link, I found that, too. I was aware of having problems when counting with floats (add, multiply...), but with just creating them, I was completly shocked!!!
Look at this:

Result:
entered: 520535.05 - effective: 520535.06
entered: 520535.06 - effective: 520535.06
entered: 520535.07 - effective: 520535.06
entered: 520535.08 - effective: 520535.1
entered: 520535.09 - effective: 520535.1
entered: 520535.10 - effective: 520535.1
entered: 520535.11 - effective: 520535.12
entered: 520535.45 - effective: 520535.44
entered: 520535.75 - effective: 520535.75
entered: 520535.85 - effective: 520535.84
entered: 520535.95 - effective: 520535.94
entered: 520535.98 - effective: 520535.97
entered: 520535.99 - effective: 520536.0
entered: 520536.00 - effective: 520536.0
entered: 520536.01 - effective: 520536.0
entered: 520536.02 - effective: 520536.03

What sence does it make to use float!!! In my opinion this field type is completly useless!!!

Stephan
[ September 01, 2005: Message edited by: Stephan Staeheli ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Floats and Doubles are good for approximating very large or small numbers. Use them for the distance between stars or the size of an electron, but not money. If you come from COBOL or some language that handles money and other fixed precisions so intuitively this can be a bit of a shocker.

Solutions include using BigDecimal or multiplying your precision out to use int or long. For example, store $1.00 as 100.
 
Jignesh Patel
Ranch Hand
Posts: 626
Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is recommanded to use BigDecimal.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!