Results 1 to 4 of 4
  1. #1
    chamalsl is offline Member
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Default Put float value to double

    Hi,

    I want to put a float value to double variable.


    float f = 1.0000345f;
    double d = f;
    System.out.println("float " + f);//Prints 1.0000345
    System.out.println("double " + d);//Prints 1.0000344514846802



    Why does the double variable contains a different value.
    Is there a way to overcome this issue.

    Thanks,
    Chamal.

  2. #2
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    6

    Default

    ^_^ this may enlighten you

    https://java.sun.com/docs/books/tuto...datatypes.html


    float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in section 4.2.3 of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.

    double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in section 4.2.3 of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  3. #3
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    Quote Originally Posted by chamalsl View Post
    Why does the double variable contains a different value. Is there a way to overcome this issue.
    This is not a bug. Its a feature of floating point. The values are essentially the same for any practical use, but they are printed differently.

    If you are using floating point, you need to think about "essentially the same value" rather than "the same"

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. String to Float
    By durahman in forum New To Java
    Replies: 2
    Last Post: 02-12-2008, 12:17 AM
  2. Float to String
    By mew in forum New To Java
    Replies: 4
    Last Post: 12-29-2007, 05:08 PM
  3. Replies: 2
    Last Post: 11-16-2007, 01:24 PM
  4. Help with valid declarations of a float
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 10:28 AM
  5. Class float
    By Peter in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •