• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

round()

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ramaseshan.
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic