Re: round number to nearest integer.

From: Sudsy (bitbucket44_at_hotmail.com)
Date: 06/05/04


Date: Sat, 05 Jun 2004 00:14:23 -0400

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.



Relevant Pages

  • RE: Rounding of floating point numbers
    ... what I am trying to do, I don't think "banker's rounding" will work. ... The total number of patterns I need will vary each time the ... The rule I must follow for odd numbers is to divide by two, round up ... nearest integer, numbers with .51 are always rounded up ...
    (perl.beginners)
  • Re: Rounding a floating point number
    ... snip float rounding discussion ... ... functions round to nearest integer. ... but I am claiming that using the C99 functions to round to N ...
    (comp.lang.c)
  • Re: Rounding a floating point number
    ... snip float rounding discussion ... ... functions round to nearest integer. ... but I am claiming that using the C99 functions to round to N ...
    (comp.lang.c)
  • Re: Round to Nearest Integer in Access
    ... always round to the nearest integer. ... For gimmicky supermarket 'rounding ...
    (microsoft.public.access.queries)
  • Re: Math.Floor
    ... An Application of method.floor is rounding a value to the nearest integer. ... double value and uses the preceding statement to round each of the numbers to the nearest integer. ... You can get the numbers out of the tokens using Double.valueOf. ... public static void main{ ...
    (comp.lang.java.programmer)