Tuesday, 27 May 2014

Difference between Math.floor() and Math.round() 0

Syntax 
 Math.floor(x) , Math.round(x)

Definition

  • Math.floor(x) and Math.round(x)  expects floating number as parameter.
  • Math.floor(x) 
    • The floor() method rounds a number DOWNWARDS to the nearest integer, and returns the result.
    • If the passed argument is an integer, the value will not be rounded.
  • Math.round(x) 
    • The round() method rounds a number to the nearest integer.
Main Difference 

  • Math.floor(x)  
    • If the parameter is 3.51, it will be rounded DOWNWARDS (out put 3).
  • Math.round(x) 
    • If the parameter is 3.49, it will be rounded DOWNWARDS (out put 3).
    • If the parameter is 3.50, it will be rounded UPWARDS (out put 4).
DEMO
Have any doubt, feel free to comment here!

0 comments:

Post a Comment