Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Math processing
Expertise: Beginner
Nov 2, 1999



Application Security Testing: An Integral Part of DevOps

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) = 16
Math.ceil(15.3) = 15

This always rounds up to the larger whole number.


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

This always rounds down, effectively removing the decimals.


Math.floor(15.65) = 15
Math.floor(15.3) = 15
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date