Solved

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

Posted on 2006-11-03
4
644 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?
0
Comment
Question by:bemara57
4 Comments
 
LVL 30

Accepted Solution

by:
callrs earned 500 total points
ID: 17869604
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
 
LVL 9

Expert Comment

by:pallosp
ID: 17870815
You may also be interested in Math.floor and Math.ceil functions.

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

Expert Comment

by:rama_krishna580
ID: 17873598
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
 
LVL 17

Expert Comment

by:gops1
ID: 17879799
You can use Math.ceil(yournumber)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 members asked questions and received personalized solutions in the past 7 days.

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

Join & Ask a Question