round number to nearest integer.

Discussion in 'Java' started by Fred, Jun 5, 2004.

  1. Fred

    Fred Guest

    I received some help here a week ago about rounding up numbers using
    Math.ceil. Unfortunately it turns out that I need to round up/down numbers
    to their nearest integer.

    Example 107200.01 needs to be rounded down to 107200.
    107200.99 neads to be rounded up to 107201.

    Is there any way to accomplish this?


    Original code:

    MPoriginalFormat2= (int) Math.ceil(MPoriginalFormat);
     
    Fred, Jun 5, 2004
    #1
    1. Advertising

  2. Fred

    Sudsy Guest

    Fred wrote:
    > I received some help here a week ago about rounding up numbers using
    > Math.ceil. Unfortunately it turns out that I need to round up/down numbers
    > to their nearest integer.
    >
    > Example 107200.01 needs to be rounded down to 107200.
    > 107200.99 neads to be rounded up to 107201.
    >
    > Is there any way to accomplish this?
    >
    >
    > Original code:
    >
    > MPoriginalFormat2= (int) Math.ceil(MPoriginalFormat);


    From the javadocs for java.lang.StrictMath:

    round

    public static long round(double a)

    Returns the closest long to the argument. The result is rounded to
    an integer by adding 1/2, taking the floor of the result, and casting
    the result to type long. In other words, the result is equal to the
    value of the expression:

    (long)Math.floor(a + 0.5d)


    Give you any ideas?

    ps. There's also a method by the same name which takes a float argument
    and returns an int.
     
    Sudsy, Jun 5, 2004
    #2
    1. Advertising

  3. Fred

    Roedy Green Guest

    On Sat, 05 Jun 2004 03:59:09 GMT, "Fred" <> wrote or
    quoted :

    >
    >Example 107200.01 needs to be rounded down to 107200.
    >107200.99 neads to be rounded up to 107201.
    >
    >Is there any way to accomplish this?


    see http://mindprod.com/jgloss/round.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jun 5, 2004
    #3
  4. Fred

    Fred Guest

    Thanks. I actually thought of this last night. Although I was going to
    subtract .5 and then round up the result.

    Simpler to use the round function though.

    "Sudsy" <> wrote in message
    news:...
    > Fred wrote:
    > > I received some help here a week ago about rounding up numbers using
    > > Math.ceil. Unfortunately it turns out that I need to round up/down

    numbers
    > > to their nearest integer.
    > >
    > > Example 107200.01 needs to be rounded down to 107200.
    > > 107200.99 neads to be rounded up to 107201.
    > >
    > > Is there any way to accomplish this?
    > >
    > >
    > > Original code:
    > >
    > > MPoriginalFormat2= (int) Math.ceil(MPoriginalFormat);

    >
    > From the javadocs for java.lang.StrictMath:
    >
    > round
    >
    > public static long round(double a)
    >
    > Returns the closest long to the argument. The result is rounded to
    > an integer by adding 1/2, taking the floor of the result, and casting
    > the result to type long. In other words, the result is equal to the
    > value of the expression:
    >
    > (long)Math.floor(a + 0.5d)
    >
    >
    > Give you any ideas?
    >
    > ps. There's also a method by the same name which takes a float argument
    > and returns an int.
    >
     
    Fred, Jun 6, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    5
    Views:
    42,719
    Steve
    May 17, 2004
  2. tertius

    round() to nearest .05 ?

    tertius, Nov 15, 2004, in forum: Python
    Replies:
    4
    Views:
    781
    tertius
    Nov 16, 2004
  3. Don
    Replies:
    30
    Views:
    6,276
    murari garg
    Mar 4, 2007
  4. round to the nearest power of 4

    , Jan 29, 2007, in forum: C Programming
    Replies:
    4
    Views:
    465
    David T. Ashley
    Jan 29, 2007
  5. noydb

    round down to nearest number

    noydb, Feb 10, 2012, in forum: Python
    Replies:
    14
    Views:
    735
    Hrvoje Niksic
    Feb 11, 2012
Loading...

Share This Page