• 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

Math.round()

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How come Math.round(9.5) = 10, but Math.round(-9.5) = -9, and yet Math.round(-9.6) = -10 ?
-Thanks
Varsha
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Math.round() will always roun up to nearest high value.
For e.g
Math.round(9.4)=9 // any value <9.5 will be rounded to 9<br /> Math.round(9.5)=10 // any value >=9.5 will be rounded to 10
Math.round(9.6)=10 //
Math.round(-9.5)= -9 // Note here -9.5 > -9.6 hence all the
numbers which are >=-9.5 will be
rounded to -9 which is a bigger number
than -10.

Math.round(-9.4)= -9 // -9.4>-9.5 hence -9
Math.round(-9.6)=-10 // since -9.6<-9.5<br /> Remember that -9 >-10
I hope u r clear

solaiappan
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Varsha,
Math.round() Rounds nearest int which is >0.5 ie if it is 9.0 to 9.5 you get 9.0 and if it is 9.6 to 9.9 you get 10.please remember sign is retained.Hope this helps.
kamesh
 
kamesh vadarevu
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OOPs I am wrong thank you soliappan.Varsha please check soliappan 's reply.
kamesh
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Varsha.The general rule about rounding is :
round() will give the closest result to the argument passed to it. So from 0.0 to 0.4 it will give 0.0(nearest lower number) and from 0.5 to 1.0 it will give 1.0(nearest higher number).When u give a middle range argument(like 0.5) it will give the next higher number(like 1.0).
For negative numbers , -9 is greater than -9.5 thus u get round(-9.5) as -9 which is nearest higher number.
For -9.6 the nearest lower number is -10.0 thus round(-9.6) is -10.


------------------
Come on in !! Drinks are on the house in the Big Moose Saloon !!
 
Varsha Dighe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou very much to all for taking time to reply to my question. I think I understand it now
-Varsha
 
reply
    Bookmark Topic Watch Topic
  • New Topic