Solved

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

Posted on 2006-11-03
4
646 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Provide the IDEA of External JS 1 35
href return to normal window 5 28
Clear input text 15 26
Jquery driving me nuts... 14 27
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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

735 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