This week's book giveaway is in the JavaScript forum.We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!See this thread for details.
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 function doubt

Vidyavathi saravanan
Ranch Hand
Posts: 34

Answer is given as -5,6,-5,5.
My doubt is the 3 rd output.
Math.round(-5.49f)--- how come this is -5. As per my understanding it is -6.
Could anyone please explain this. Its confusing me a lot.

Thanks.

Nina Binde
Ranch Hand
Posts: 85
float f6 = -5.49f;

round always rounds a float or a double to the nearest integer or a long respectively. If it is right in the middle like 5.5, it rounds to the nearest large integer.
For the above example, -5.49 is closer to -5 than it is to -6. Hence , the output.

The answer would be -6 if f6 = -5.51f.

Get it?

Jay Pawar
Ranch Hand
Posts: 411
Vidyavathi,
If you look the underlying implementation of round , it is nothing but adding 0.5f to the number and then perform floor on the result. In your case,

I know this round function is confusing sometimes, but if you know floor method correctly then you will never fail in getting answer for round function. Hope this helps ya.
[ November 11, 2004: Message edited by: Jay Pawar ]