Click to See Complete Forum and Search --> : converting a string to number


davidcholley
10-24-2003, 07:33 PM
I'm trying to write a function in JavaScript that will automatically increment the value on a form field. The problem that I am having is that the code is concatenating instead of performing a summation. Any help?

noOfCars = document.getElementById('noOfCars').value

if(noOfCars < maxNoOfPax){
document.getElementById('noOfCars').value = noOfCars + 1
}

So, I suppose that the first question is - what is the best way of working with a numeric value obtained from a form?

David H

Khalid Ali
10-24-2003, 08:19 PM
instead use it like below

document.getElementById('noOfCars').value = parseInt(noOfCars) + 1

davidcholley
10-25-2003, 07:31 AM
My next question then is this...

What is the best way to always round a number UP to the next higher integer as opposed to the nearest whole integer? For example, if result of a calculation is 5.00001, I need the result to be rounded to 6.

David

Khalid Ali
10-25-2003, 07:51 AM
You are welcome.

var num = 5.00001
alert(Math.ceil(num));

Read the docs on Math Class in JavaScript at
http://devedge.netscape.com/central/javascript/