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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Bear Bibeault
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Jj Roberts
• Carey Brown
Bartenders:
• salvin francis
• Frits Walraven
• Piet Souris

# round()

Ranch Hand
Posts: 219
• Number of slices to send:
Optional 'thank-you' note:
hi all

It has been said in K&B that in round(),if the number after the decimal point is greater than or equal to 0.5 then Math.round() is equal to Math.ceil()

The output is -4.
How come the output is -4 it should be -3 .Am i right.

Ranch Hand
Posts: 30
• Number of slices to send:
Optional 'thank-you' note:
To find rounded value, always use floor(value+0.5). In this case
floor(-3.6+0.5)=floor(-3.1)=-4.

Refer Math API..

meena latha
Ranch Hand
Posts: 219
• Number of slices to send:
Optional 'thank-you' note:
Thanks ramaseshan.

Ranch Hand
Posts: 172
• Number of slices to send:
Optional 'thank-you' note:
the easiest trick is ...
Math.round(-3.5) is -3 ceil the number if -'ve fraction is <=5.
Math.round(-3.6) is -4 floor the number if -'ve fraction is >=5.
Math.round(3.5) is 4 The reverse strategy applies for +'ve fractions

i.e floor the number if +'ve fraction is <=5
& ceil the number if +'ve fraction is >5

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?