Hello,
is there any way how to convert String with value "11013672.7100" into coresponding float value. When I use Float.valueOf("11013672.7100") I get 1.1013673E7, but I need float with value 11013672.7100. Is this possible? Maybe this is somehow related to value I am trying to convert.

You are getting the float correctly parsed. Two questions back:

1: Why are you using floats if you want any sort of precision?

2: Do you realise that there is a difference between the value of the float and how it is displayed?

If you want precision, there is a precise class for arithmetic: BigDecimal(=link). You can find more about it in this thread.

Boris Belovic
Greenhorn

Joined: Nov 27, 2009
Posts: 14

posted

0

@1 I am using float because of our interface design (which I admit is poor). Internally, in method of this interface I am using BigDecimal for computation, but I have to return float value from methods. So, using BigDecimals I have computed value of 11013672.7100 and now I need to convert it to corresponding float value. But it seems that the BigDecimal's value and float value (value after conversion) differs.

@2 I didn't know that. So how should I correctly display floats?

Boris Belovic wrote:@1 I am using float because of our interface design (which I admit is poor). Internally, in method of this interface I am using BigDecimal for computation, but I have to return float value from methods. So, using BigDecimals I have computed value of 11013672.7100 and now I need to convert it to corresponding float value. But it seems that the BigDecimal's value and float value (value after conversion) differs.

With BigD, you get an arbitrary (specified by you) degree of decimal precision.

With float you get fixed (32 bits) binary precision. For a trivial example, a float cannot store the value 0.1.

@2 I didn't know that. So how should I correctly display floats?

@ Jeff I know, but using BigDecimal I have computed the value 11013672.71, the thing is how to convert it to correct float value. To me it seems the value after conversion is rounded to 11013673.

Boris Belovic wrote:@ Jeff I know, but using BigDecimal I have computed the value 11013672.71, the thing is how to convert it to correct float value.

It depends what you mean by "correct." It is impossible to store that value in a float. (And note that this is not a Java issue per se. It's a consequence of floating point representations in general and the IEEE 754 spec in particular.)

To me it seems the value after conversion is rounded to 11013673.

Then that's probably correct, because float's 32 digits of decimal precision (31 on either side of 0) corresponds to somewhat less than 10 digits of decimal precision.

If you think about it for a minute, you'll realize that there have to be some int values that cannot be stored in float, and that if you're trying to count by +1 using float, you will have to skip several int values at some point.

Boris Belovic wrote:@ Jeff I know, but using BigDecimal I have computed the value 11013672.71, the thing is how to convert it to correct float value. To me it seems the value after conversion is rounded to 11013673.

First thing to do is check that assumption. You could just be running out of displayed digits in your notation. Use the formatting methods Jeff showed you to define more digits to see if it is getting rounded or not.

Jeff Verdegan wrote:
If you think about it for a minute, you'll realize that there have to be some int values that cannot be stored in float, and that if you're trying to count by +1 using float, you will have to skip several int values at some point.

Boris Belovic wrote:@1 I am using float because of our interface design (which I admit is poor). Internally, in method of this interface I am using BigDecimal for computation, but I have to return float value from methods. So, using BigDecimals I have computed value of 11013672.7100 and now I need to convert it to corresponding float value. But it seems that the BigDecimal's value and float value (value after conversion) differs.

It shouldn't if you're using floatValue(), which all Java Number's, including BigDecimal, share.

If it is does, it's probably because you haven't read this.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034

28

posted

0

Jeff Verdegan wrote: . . . 32 digits of decimal precision (31 on either side of 0) corresponds to somewhat less than 10 digits of decimal precision. . . .

You are a bit over‑optimistic about the precision of floats. IEEE754 or whatever uses 1 digit for sign, 8 digits as exponent and 24 digits as mantissa. 24 × log(2) = 7.22, so you get at best 7¼ digits in decimal.

And 1 + 8 + 24 = 33. I know. It is supposed to add up to 33.

Jeff Verdegan wrote: . . . 32 digits of decimal precision (31 on either side of 0) corresponds to somewhat less than 10 digits of decimal precision. . . .

You are a bit over‑optimistic about the precision of floats. IEEE754 or whatever uses 1 digit for sign, 8 digits as exponent and 24 digits as mantissa. 24 × log(2) = 7.22, so you get at best 7¼ digits in decimal.

And 1 + 8 + 24 = 33. I know. It is supposed to add up to 33.