The two signatures of round() are: public static int round(float) public static long round(float)
This is something very important to memorize for the exam because it will be on it: round(),ceil(), and floor() take floats and return "integer equivalents". ceil() and floor() actually return a double.
K&B book says ceil(), floor() and round() all return integer equivalent floating-point numbers. But the following code prints
System.out.println(Math.round(1.1)); //prints 1 System.out.println(Math.ceil(1.1)); // prints 2.0
The first line should print 1.0, correct? Thanks.
Chitra, Math.round() returns int or long depending on the argument passed to it. i.e Math.round(double d) will return long whereas Math.round(float f) would return you int data type. Math.ceil(double d) always returns double.
Check this link for Math functions. Hope that helps...
Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
So the rule for round() is you add 0.5 to the argument and then cut off whatever is after the decimal. So if you start with round(3.2), you add 0.5 and get 3.7, then cut off everything after the decimal to get 3.