Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 650
  • Last Modified:

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

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?
0
bemara57
Asked:
bemara57
1 Solution
 
callrsCommented:
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
0
 
pallospCommented:
You may also be interested in Math.floor and Math.ceil functions.

Short description is here:
http://www.javascripter.net/faq/mathfunc.htm
0
 
rama_krishna580Commented:
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
0
 
gops1Commented:
You can use Math.ceil(yournumber)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now