Round to a Whole Number

Round to a Whole Number


I have a program that calculates the number of people the government could send to college with the money they spend on certain defense programs. The script works, but when I divide the cost of the education into the cost of the program, I get a number with lots of decimal places. As you know, there is no such thing as .5647389 of a person, so is there a way to lop that decimal off of my answer?


There are three ways to convert a number with decimals into a whole number. Which method you use depends on how you’d like to do the conversion.

This is the most common way to turn a number with decimals into a whole number. The return value is the nearest whole number. Technically, if the decimal portion of the number is 0.5 or greater the method returns the smallest integer that is greater than the number, otherwise the largest integer that’s less than or equal to number is returned. Note that this may not be what you expect for negative numbers.


Math.round(15.65) = 16Math.ceil(15.3) = 15

This always rounds up to the larger whole number.


Math.ceil(15.65) = 16Math.ceil(15.3) = 16

This always rounds down, effectively removing the decimals.


Math.floor(15.65) = 15Math.floor(15.3) = 15


Share the Post: