Solved

JQuery to round up numbers

Posted on 2014-09-15
2
332 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 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
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
Comment Utility
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

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to dynamically set the form action using jQuery.

728 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

9 Experts available now in Live!

Get 1:1 Help Now