Forums Register Login

Float conversion

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Float.parseFloat(String)

A quick google search would have given you the answer really.
+Pie Number of slices to send: Send
Have you tried it? For Float.parseFloat(""11013672.71"") I am getting 1.1013673E7 which isn't what I need.
+Pie Number of slices to send: Send
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.
    +Pie Number of slices to send: Send
    @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?

    +Pie Number of slices to send: Send
     

    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.


    +Pie Number of slices to send: Send
    @ 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.
    +Pie Number of slices to send: Send
     

    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.
    +Pie Number of slices to send: Send
     

    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.
    +Pie Number of slices to send: Send
     

    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.



    For example:
    +Pie Number of slices to send: Send
     

    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
    +Pie Number of slices to send: Send
     

    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.
    +Pie Number of slices to send: Send
     

    Campbell Ritchie wrote:

    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.



    Yeah, I was being lazy.

    Hope that didn't cause any further confusion.
    +Pie Number of slices to send: Send
     

    Jeff Verdegan wrote: . . .

    Hope that didn't cause any further confusion.

    I don’t think you did.
    I'm sure glad that he's gone. Now I can read this tiny ad in peace!
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 1059 times.
    Similar Threads
    string to float conversion round off problem
    floats and Float objects
    what this signature means?
    About default values in array
    float vs int
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 28, 2024 08:04:04.