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

x
?
Solved

JQuery to round up numbers

Posted on 2014-09-15
2
Medium Priority
?
441 Views
Last Modified: 2014-09-16
How do I round up a number in JQuery?

If I have a var = 1.56 and I want to round that up to 2.
var base = 1.56;

//  I wanted to write back into a form with the input name damage and write its value as 2? 

document.getElementById('damage').value = base;

Open in new window

0
Comment
Question by:rgranlund
2 Comments
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 40323860
var base = 1.56;

//  I wanted to write back into a form with the input name damage and write its value as 2?

document.getElementById('damage').value = Math.round(base);
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 40324803
Hi ,

Just some addition to the Leakim971's answer.

If you want  to convert decimal value to the nearest integer then use :

document.getElementById('damage').value = Math.round(base);

If you want to convert decimal value always to the upper integer the use:

document.getElementById('damage').value = Math.ceil(base);
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
Suggested Courses

824 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