This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Float conversion

 
Boris Belovic
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dhaval J. Patel
Ranch Hand
Posts: 90
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Float.parseFloat(String)

A quick google search would have given you the answer really.
 
Boris Belovic
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it? For Float.parseFloat(""11013672.71"") I am getting 1.1013673E7 which isn't what I need.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @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?

     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @ 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.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Steve Luke
    Bartender
    Posts: 4181
    21
    IntelliJ IDE Java Python
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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:
     
    Winston Gutkowski
    Bartender
    Pie
    Posts: 10247
    58
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Campbell Ritchie
    Sheriff
    Posts: 48652
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Campbell Ritchie
    Sheriff
    Posts: 48652
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeff Verdegan wrote: . . .

    Hope that didn't cause any further confusion.
    I don’t think you did.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic