# Math.round...(for -ve no.)

ratul banji

Ranch Hand

Posts: 108

posted 14 years ago

- 0

Hi,

public class math

{

public static void main(String a[])

{

System.out.println(Math.round(-2.5));// o/p is -2....line 1

System.out.println(Math.round(-2.4));// o/p is -2....line 2

System.out.println(Math.round(-2.6));// o/p is -3....line 3

System.out.println(Math.round(2.5)); // o/p is 3...as expected.

System.out.println(Math.round(2.4)); // o/p is 2...as expected.

System.out.println(Math.round(2.6)); // o/p is 3...as expected.

}

}

round returns a long for double, returns an int for float. (closest int or long value to the argument)

The result is rounded to an integer by adding � , taking the floor of the result, and casting the result to type int / long.

NOw...look at the o/p of line 1 and line 3 (-2.5 is giving -2 whereas line -2.6 is giving -3.) . As fer as in +ve no. this is not the rule. In positive nos. both 2.5 and 2.6 will give 3.Now look at line 2 also.

same problem here.

Can any1 tell me what is real rule for -ve no's roundness..

I am simply not geting any rule??

Thanks In Advance.

<marquee>

public class math

{

public static void main(String a[])

{

System.out.println(Math.round(-2.5));// o/p is -2....line 1

System.out.println(Math.round(-2.4));// o/p is -2....line 2

System.out.println(Math.round(-2.6));// o/p is -3....line 3

System.out.println(Math.round(2.5)); // o/p is 3...as expected.

System.out.println(Math.round(2.4)); // o/p is 2...as expected.

System.out.println(Math.round(2.6)); // o/p is 3...as expected.

}

}

round returns a long for double, returns an int for float. (closest int or long value to the argument)

The result is rounded to an integer by adding � , taking the floor of the result, and casting the result to type int / long.

NOw...look at the o/p of line 1 and line 3 (-2.5 is giving -2 whereas line -2.6 is giving -3.) . As fer as in +ve no. this is not the rule. In positive nos. both 2.5 and 2.6 will give 3.Now look at line 2 also.

same problem here.

Can any1 tell me what is real rule for -ve no's roundness..

I am simply not geting any rule??

Thanks In Advance.

<marquee>

**Ratul Banerjee </marquee>**
Art Metzer

Ranch Hand

Posts: 241

posted 14 years ago

- 0

Hi, Ratul.

Math.round() returns the integral value (int or long) that is closest to the input value. If that input value is equidistant from its neighboring integral values (e.g., -2.5, +104.5), Math.round() rounds up, towards positive infinity. Hence Math.round( -2.5 ) yields -2, and Math.round( +2.5 ) yields 3.

Art

Math.round() returns the integral value (int or long) that is closest to the input value. If that input value is equidistant from its neighboring integral values (e.g., -2.5, +104.5), Math.round() rounds up, towards positive infinity. Hence Math.round( -2.5 ) yields -2, and Math.round( +2.5 ) yields 3.

Art

ratul banji

Ranch Hand

Posts: 108

Art Metzer

Ranch Hand

Posts: 241

posted 14 years ago

- 0

Because -3 is the int that is closer to the float -2.6. As I mentioned earlier, as a general rule, Math.round() returns the integral value that is closer to the input value. The int closer to -2.6 is -3. Similarly, the int closer to +2.6 is +3, so Math.round( +2.6 ) = 3.

Now that we've established that round() returns the integral value that's closer to the input, the question presents itself, what is the behavior if the input value is

Maybe this illustration will help:

If this hasn't cleared round() up for you, Ratul, let me know.

Art

Now that we've established that round() returns the integral value that's closer to the input, the question presents itself, what is the behavior if the input value is

*equidistant*from its neighboring integral values? Which int is "closer" then? That is, if float f = ( 2n + 1 ) / 2 for integral n, what is Math.round( f )? The answer is, for these*special cases*where round()'s input is "something-point-five", round() rounds up towards positive infinity.Maybe this illustration will help:

If this hasn't cleared round() up for you, Ratul, let me know.

Art

shadow liu

Ranch Hand

Posts: 33