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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s