# Float conversion

Boris Belovic

Greenhorn

Posts: 14

posted 4 years ago

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.

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.

Boris Belovic

Greenhorn

Posts: 14

Campbell Ritchie

Sheriff

Posts: 50770

83

posted 4 years ago

You are getting the1: Why are you using 2: Do you realise that there is a difference between the value of the If you want precision, there is a precise class for arithmetic: BigDecimal(=link). You can find more about it in this thread.

`float`correctly parsed. Two questions back:`float`s if you want any sort of precision?

`float`and how it is displayed?

Boris Belovic

Greenhorn

Posts: 14

posted 4 years ago

@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?

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

posted 4 years ago

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.

System.out.printf() or String.format() (also see format string syntax for those two) or java.text.DecimalFormat. Pick whichever works best for your needs.

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?

System.out.printf() or String.format() (also see format string syntax for those two) or java.text.DecimalFormat. Pick whichever works best for your needs.

Boris Belovic

Greenhorn

Posts: 14

posted 4 years ago

@ 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.

posted 4 years ago

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.)

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.

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.

posted 4 years ago

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.

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.

Steve

posted 4 years ago

It shouldn't if you're using

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

Winston

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

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Campbell Ritchie

Sheriff

Posts: 50770

83

posted 4 years ago

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

You are a bit over‑optimistic about the precision ofJeff Verdegan wrote: . . . 32 digits of decimal precision (31 on either side of 0) corresponds to somewhat less than 10 digits of decimal precision. . . .

`float`s. 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.

posted 4 years ago

Yeah, I was being lazy.

Hope that didn't cause any further confusion.

Campbell Ritchie wrote:You are a bit over‑optimistic about the precision ofJeff Verdegan wrote: . . . 32 digits of decimal precision (31 on either side of 0) corresponds to somewhat less than 10 digits of decimal precision. . . .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.

Yeah, I was being lazy.

Hope that didn't cause any further confusion.