round number to nearest integer.

F

Fred

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);
 
S

Sudsy

Fred said:
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.
 
F

Fred

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top