Results 1 to 5 of 5
  1. #1
    shihad_s is offline Member
    Join Date
    Dec 2010
    Posts
    14
    Rep Power
    0

    Default data of more than 7 digits is rounded up by 1

    Hi,
    I have a small doubt , when using 'Float', data of more than 7 digits is rounded up by 1.
    piece of code is given below:
    <code>

    String _val = "51300123";
    Float f = new Float (_val);
    System.out.println(f);

    </code>

    I am getting output as 5.1300124E7 .
    But when i tried with 7 digits ie; when put _val = "5130012"; getting o/p as 5130012.0
    This problem occurs only when there are more than 7 digits.
    Can anyone help me in resolving this. I need to get the exact Output without rounding..
    It is Urgent

    Thanks,
    Shihad

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Don't include urgent in your posts. Floating point is not exact and will always cause problems.

    Run this to see how it looks
    Java Code:
    for(float f = 0; f < 1; f+=.1){
      System.out.println(f);
    }

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by shihad_s View Post
    Hi,
    I have a small doubt , when using 'Float', data of more than 7 digits is rounded up by 1.
    piece of code is given below:
    <code>

    String _val = "51300123";
    Float f = new Float (_val);
    System.out.println(f);

    </code>

    I am getting output as 5.1300124E7 .
    But when i tried with 7 digits ie; when put _val = "5130012"; getting o/p as 5130012.0
    This problem occurs only when there are more than 7 digits.
    Can anyone help me in resolving this. I need to get the exact Output without rounding..
    It is Urgent
    Oh dear, I hope I'm not too late ... read the Fload class API documentation: a float uses 23 bits for its mantissa; this means that it can represent the numbers in the range [0, 2^23) exactly (shifted right or left optionally);2^23 is a bit larger than 8 million, that explains it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I need to get the exact Output without rounding.

    System.out.println(_val); won't do any rounding...

    More seriously, Oracle's Tutorial on the Types, Values and Variables page reccommends not using float for precise values. It gives links explaining how to use BigDecimal and how to format numeric values.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

Similar Threads

  1. Replies: 1
    Last Post: 01-19-2011, 09:23 PM
  2. Create a rounded rectangle in a panel
    By radhi in forum Java 2D
    Replies: 5
    Last Post: 10-05-2010, 08:58 AM
  3. Rounded Rectangle
    By Arnold in forum Android
    Replies: 0
    Last Post: 05-27-2010, 04:06 PM
  4. Replies: 6
    Last Post: 06-12-2008, 09:14 AM
  5. Rectangle with rounded edges??
    By orchid in forum Java 2D
    Replies: 1
    Last Post: 05-10-2007, 02:31 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
  •