We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

# How do I round decimal number into an integer with Javascript?

bemara57 asked
on
Medium Priority
665 Views
Last Modified: 2008-01-09
I'm feeding a variable from a database which comes in with three decimal places. But I want to redisplay the variable as an integer. How do I do that?
Comment
Watch Question

## View Solution Only

Awarded 2006
Top Expert 2006
Commented:
From: http://www.javascriptkit.com/javatutors/round.shtml  Number rounding in JavaScript

Math.round(x)

Using it, any supplied argument is rounded off to the nearest integer, and using the ".5" up rule. For example:

Math.round(25.9) //returns 26
Math.round(25.2) //returns 25
Math.round(-2.58) //returns -3

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
You may also be interested in Math.floor and Math.ceil functions.

Short description is here:
http://www.javascripter.net/faq/mathfunc.htm

Commented:
Hi,

http://www.mediacollege.com/internet/javascript/number/round.html

<script language="javascript" type="text/javascript">
function roundNumber() {
var numberField = document.roundform.numberfield; // Field where the number appears
var rnum = numberField.value;
var rlength = 2; // The number of decimal places to round to
if (rnum > 8191 && rnum < 10485) {
rnum = rnum-5000;
var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
newnumber = newnumber+5000;
} else {
var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
}
numberField.value = newnumber;
}
</script>
<form name="roundform">
<input type="text" name="numberfield">
<input type="button" value="Round" onClick="roundNumber();">
</form>

http://www.jsmadeeasy.com/javascripts/Calculators/Round/index.htm
http://www.javascriptkit.com/script/script2/roundnum.shtml

R.K

Commented:
You can use Math.ceil(yournumber)
Access more of Experts Exchange with a free account
##### Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the