Results 1 to 6 of 6
  1. #1
    JMaste is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default Error in float value

    Hi,

    I am a newbie in JAVA. When I wrote the following code in java it gives me error:

    public class FloatTest
    {
    public static void main(String[] args)
    {
    float f=3.14;
    System.out.println(f);
    }
    }

    It gives me an error saying possible lose of precision. Aren't float value capable of storing values with decimal point?

    Thank you.

  2. #2
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    You need to use double i think, also try to use
    Java Code:
     code here
    tags

    # 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.
    Last edited by coltragon; 07-11-2010 at 03:15 PM.
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    The compiler gives 3.14 the type of double. Assigning a double to a float could give "possible lose of precision".
    If you want a float literal append an F: 3.14F

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default

    A bit more clarification: when the compiler sees the textual representation of a number with decimal fractions (a dot followed by more digits) it assumes a number of type double. If you want to assign that number to a variable of type float the compiler starts whining because you might lose precision (some fractional digits).

    There are two ways to solve this little issue:

    1) Tell the compiler you are dealing with float numbers by appending a single 'f' to your number, i.e. 3.14f or
    2) use variables of type double instead of type float.

    kind regards,

    Jos

    edit: I'm just a slow old sod ... ;-)

  5. #5
    JMaste is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default Float error

    Thanks very much for your answers... :)

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

    Default

    Cross-post: http://forums.sun.com/thread.jspa?messageID=11017870
    To the original poster, welcome to the forum, and I'm glad that your question got answered, but I do request that if you cross-post a question you provide links to the other cross-posts. Nobody likes duplicating work that's already been done. Thanks for your cooperation as this question has already been answered in the Sun fora.
    Last edited by Fubarable; 07-11-2010 at 04:18 PM.

Similar Threads

  1. Working with float
    By dardar in forum New To Java
    Replies: 8
    Last Post: 01-27-2010, 08:29 PM
  2. Float vs. Double
    By javanub in forum New To Java
    Replies: 1
    Last Post: 11-23-2008, 12:11 PM
  3. Put float value to double
    By chamalsl in forum New To Java
    Replies: 3
    Last Post: 09-20-2008, 04:52 AM
  4. Error with convert to Float
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 02:18 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
  •