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

Solved

Posted on 2006-11-03

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?

4 Comments

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

Short description is here:

http://www.javascripter.net/faq/mathfunc.htm

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

<script language="javascript" type="text/javascript">

function roundNumber() {

var numberField = document.roundform.numberf

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(1

newnumber = newnumber+5000;

} else {

var newnumber = Math.round(rnum*Math.pow(1

}

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**17** Experts available now in Live!