File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Float conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Float conversion" Watch "Float conversion" New topic
Author

Float conversion

Boris Belovic
Greenhorn

Joined: Nov 27, 2009
Posts: 14
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

Joined: Mar 10, 2011
Posts: 90

Float.parseFloat(String)

A quick google search would have given you the answer really.
Boris Belovic
Greenhorn

Joined: Nov 27, 2009
Posts: 14
Have you tried it? For Float.parseFloat(""11013672.71"") I am getting 1.1013673E7 which isn't what I need.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
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
    @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

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    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

    Joined: Nov 27, 2009
    Posts: 14
    @ 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

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    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

    Joined: Jan 28, 2003
    Posts: 4181
        
      21

    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
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    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

    Joined: Mar 17, 2011
    Posts: 8008
        
      22

    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: 39409
        
      28
    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

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    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

    Joined: Oct 13, 2005
    Posts: 39409
        
      28
    Jeff Verdegan wrote: . . .

    Hope that didn't cause any further confusion.
    I don’t think you did.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Float conversion