• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Dan's question on Math.round()

 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 8 on Math


Which of the following statements is true in terms of the value returned by the java.lang.Math.round method?

a. Rounds the argument to the nearest whole number. If the result is equally close to two whole numbers, then the even one is returned.
b. Rounds the argument to the nearest whole number. If the result is equally close to two whole numbers, then the odd one is returned.
c. Adds 0.5 to the argument and takes the floor of the result.
d. Adds 0.5 to the argument and takes the ceil of the result.
e. None of the above



The answer given is c. The Java API is more specific in saying that

Returns the closest int 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 int. In other words, the result is equal to the value of the expression:

(int)Math.floor(a + 0.5f)

and similarly for double-long combination.
reply
    Bookmark Topic Watch Topic
  • New Topic