Solved

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

Posted on 2006-11-03
4
638 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Form submit issue 11 56
Importing and exporting data Magento 1.x ? 4 38
Adding Extra Information box 4 26
Adjust the position 3 14
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now