• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Conversion problem

 
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
 
Master Rancher
Posts: 4068
37
  • 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.
 
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 ]
 
(instanceof Sidekick)
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.
 
Ranch Hand
Posts: 646
Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is recommanded to use BigDecimal.
 
This tiny ad will self destruct in five seconds.
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic