Posted in Interesting, Programming, Web Tech

Round, ceil, floor : Decimal to integer

Javascript has inbuilt functions for converting decimal number to an integer value.

Math.round function is used to round a number to its nearest integer. If fractional part of the number >=0.5 then the argument is rounded to next higest integer.
If fractional part of the number < 0.5 then the argument is rounded to lowest integer.
Syntax: Math.round(number)
eg. Math.round(7.5) = 8
Math.round(7.2) = 7

Math.ceil function returns integer greater than or equal to a given number. Ceil means think of the ceiling of the room which is above your head.
Syntax: Math.ceil(number)
eg. Math.ceil(7.5) = 8
Math.ceil(7.2) = 8
Math.ceil(7) = 7
Math.ceil(-7.5) = -7
Math.ceil(-7.2) = -7

Math.floor function returns integer lesser than or equal to a given number. Floor means think of the floor of the room which is below you
Syntax: Math.floor(number)
eg. Math.floor(7.5) = 7
Math.floor(7.2) = 7
Math.floor(7) = 7
Math.floor(-7.5) = -8
Math.floor(-7.2) = -8