Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Math.round() returns what ?

Sherry Jacob
Ranch Hand
Posts: 128
I have a problem,
I studied using the Khalid Mughal book. This book says that the Math.round() function returns an int.

However, while testing myself on MindQ's mock exam, I came across a question which says, What method(s) from the java.lang.Math class might method() be if the statement
method( -4.4 ) == -4; is true ?

The options were : round(), min(), trunc(), abs(), floor(), ceil().

I marked round() according to what Khalid's book says. But the answer is round(), floor() and ceil() !!

Joseph Clark
Ranch Hand
Posts: 48
Math.floor(-4.4) returns -5.0, which the '==' operator will narrowly convert to int when compared against -4 (and will result in a false for the comparison).

Math.ceil(-4.4) returns -4.0, which will return true when compared against -4.

Math.round(-4.4) returns (long) -4, which will be true for the comparison.*

. . . will output . . .

Math.ceil(-4.4))==-4 is true
-5.0
Math.round(-4.4))==-4 is true

*Correction: -4.4 is a double, so, Math.round(-4.4) returns the 64-bit long integer instead of the int type integer.
[ September 11, 2005: Message edited by: michael clark ]

Shivani Chandna
Ranch Hand
Posts: 380
and well Math.round() returns an int when a float is passed and returns a long when double is passed....

Sandeep Chhabra
Ranch Hand
Posts: 340
Hi,