# a question from Poddar's exam

shilpa gupta

Greenhorn

Posts: 26

posted 16 years ago

this q is from poddar's mock test

{

float f4 = -5.5f;

float f5 = 5.5f;

float f6 = -5.49f;

float f7 = 5.49f;

System.out.println("Round f4 is " + Math.round(f4));

System.out.println("Round f5 is " + Math.round(f5));

System.out.println("Round f6 is " + Math.round(f6));

System.out.println("Round f7 is " + Math.round(f7));

}

and the ans is

1] Round f4 is -5

2] Round f5 is 6

3] Round f6 is -5

4] Round f7 is 5

i am unable to understand ans 1 & 3

please explain

{

float f4 = -5.5f;

float f5 = 5.5f;

float f6 = -5.49f;

float f7 = 5.49f;

System.out.println("Round f4 is " + Math.round(f4));

System.out.println("Round f5 is " + Math.round(f5));

System.out.println("Round f6 is " + Math.round(f6));

System.out.println("Round f7 is " + Math.round(f7));

}

and the ans is

1] Round f4 is -5

2] Round f5 is 6

3] Round f6 is -5

4] Round f7 is 5

i am unable to understand ans 1 & 3

please explain

Sivalingam Sivasuthan

Ranch Hand

Posts: 75

posted 16 years ago

Hi shilpa gupta:

Math.round() method

Rounds to the nearest integer. So, if the value is more than half way towards the higher integer, the value is rounded up to the next ingeter. If the number is less than this the next lowest integer is returned.

So for example if the input to round is f then :

5.0 <=f < 5.5 then Math.round(f)==5.0

5.5 <=f < 6.0 then Math.round(f)==6.0

-5.0 <=f <= -5.5 then Math.round(f)==-5.0

-5.5 < f < -6.0 then Math.round(f)==-6.0

Siva.

Math.round() method

Rounds to the nearest integer. So, if the value is more than half way towards the higher integer, the value is rounded up to the next ingeter. If the number is less than this the next lowest integer is returned.

So for example if the input to round is f then :

5.0 <=f < 5.5 then Math.round(f)==5.0

5.5 <=f < 6.0 then Math.round(f)==6.0

-5.0 <=f <= -5.5 then Math.round(f)==-5.0

-5.5 < f < -6.0 then Math.round(f)==-6.0

Siva.

- Siva<br /> Sun Certified Programmer for the Java™2 Platform

Bala Arul

Greenhorn

Posts: 29

posted 16 years ago

Hi Siva,

There is

-5.0 <=f <= -5.5 or -5.5 < f < -6.0,

since -5.0 is greater than -5.5 and -5.5 is greater than -6.0.

The correct way to find a rounded value is to find the floor value of given number plus 0.5.

So, floor(-5.5 + 0.5) = floor(-5.0) = -5.0

floor(-5.49 + 0.5) = floor(-4.99) = -5.0

NB: floor value of a number is defined as 'the highest integer value that is less than or equals to the given number.

... -7.0, -6.0, -5.0 are less than or equals to -4.99 or -5.00, but -5.0 is the highest.

Arul.

[This message has been edited by Bala Arul (edited February 16, 2001).]

Originally posted by Sivalingam Sivasuthan:

-5.0 <=f <= -5.5 then Math.round(f)==-5.0

-5.5 < f < -6.0 then Math.round(f)==-6.0

There is

**not**such number 'f' that,-5.0 <=f <= -5.5 or -5.5 < f < -6.0,

since -5.0 is greater than -5.5 and -5.5 is greater than -6.0.

The correct way to find a rounded value is to find the floor value of given number plus 0.5.

So, floor(-5.5 + 0.5) = floor(-5.0) = -5.0

floor(-5.49 + 0.5) = floor(-4.99) = -5.0

NB: floor value of a number is defined as 'the highest integer value that is less than or equals to the given number.

... -7.0, -6.0, -5.0 are less than or equals to -4.99 or -5.00, but -5.0 is the highest.

Arul.

[This message has been edited by Bala Arul (edited February 16, 2001).]

Manfred Leonhardt

Ranch Hand

Posts: 1492

posted 16 years ago

Hi all,

The answers given above are correct with one noted exception. In Java the Math.round() method return either an integer (given float parameter) or a long (given double parameter). Therefore the statement:

Math.round(f) = X.0

is not technically correct in Java. It should be:

Math.round(f) = X

Regards,

Manfred.

The answers given above are correct with one noted exception. In Java the Math.round() method return either an integer (given float parameter) or a long (given double parameter). Therefore the statement:

Math.round(f) = X.0

is not technically correct in Java. It should be:

Math.round(f) = X

Regards,

Manfred.

Bala Arul

Greenhorn

Posts: 29