Results 1 to 5 of 5
  1. #1
    IndraG is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default How to round up floating number properly?

    I just found out that converting a floating number to integer, will round down in Java, for example:
    When I try:
    float fl = 45.7;
    int i = (int) fl;
    Or
    Float fl = New Float(45.7);
    int i = fl.intValue()
    The results are = 47! How can I round it up to the nearest integer? For example 45.7 -> 46, 45.3 -> 45.

    I am sorry if I sound dumb here, I am not, but I am used to other language (which can do this easily), and the book I use do not explain this one in enough detail. Thank you...

  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 Re: How to round up floating number properly?

    Look at the round methods provided by the Math class, Math (Java Platform SE 6)

  3. #3
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: How to round up floating number properly?

    float fl = 45.7f;
    int i = Math.round(fl);

    but if You have double, need typecast to int:
    double fl = 45.7;
    int i = (int)Math.round(fl);


    Because, Math.round(fl) return (long)Math.floor(fl + 0.5) and floor(fl + 0.5) return double floor(double fl).

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

    Default Re: How to round up floating number properly?

    And maybe look at the formatting offered by NumberFormat and the printf() family (in String, PrintStream etc).

    I mention this because often people speak of "rounding" a number when they are really concerned with getting some human friendly representation of it. The latter task (which is formatting) need not involve rounding a value (and hence making it less accurate).

  5. #5
    IndraG is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: How to round up floating number properly?

    OK, Guys. Thank you so much. Got it. Much appreciated...

Similar Threads

  1. Round of help
    By micolord in forum New To Java
    Replies: 5
    Last Post: 10-08-2011, 06:56 AM
  2. Using math.round
    By Hype in forum New To Java
    Replies: 3
    Last Post: 01-31-2011, 12:57 AM
  3. if it is floating point number
    By Stephen Douglas in forum New To Java
    Replies: 26
    Last Post: 07-06-2010, 10:17 AM
  4. going round and round in class..
    By sonny in forum New To Java
    Replies: 52
    Last Post: 04-02-2010, 11:56 AM
  5. number of floating point
    By mohammad8065 in forum Advanced Java
    Replies: 5
    Last Post: 12-28-2008, 10:41 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
  •