Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Conversion problem

Stephan Staeheli
Greenhorn
Posts: 27
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

Bartender
Posts: 1480
14
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

Stephan Staeheli
Greenhorn
Posts: 27
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
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
It is recommanded to use BigDecimal.