Velocity Reviews > Java > round number to nearest integer.

# round number to nearest integer.

Fred
Guest
Posts: n/a

 06-05-2004
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);

Sudsy
Guest
Posts: n/a

 06-05-2004
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);

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.

Roedy Green
Guest
Posts: n/a

 06-05-2004
On Sat, 05 Jun 2004 03:59:09 GMT, "Fred" <(E-Mail Removed)> 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

--
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Fred
Guest
Posts: n/a

 06-06-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>