Forum Moderators: open
var p = ff_getElementByName('bor_base_income_data').value;
var q = ff_getElementByName('co_bor_base_income_data').value;
var baseincome = p + q;
ff_getElementByName('total_base_data').value = baseincome;
If these values will be integers, you could try something like this:
var p = parseInt(ff_getElementByName('bor_base_income_data').value,10);
var q = parseInt(ff_getElementByName('co_bor_base_income_data').value, 10);
var baseincome = p + q;
1/ So I understand, what is happening here, What is the 10 for? Just for argument sake, what if I used a 7?
2/ When entering the numbers in the inputbox for calculation, after I enter the first number, this throws up "NaN" in the result inputbox until the other number is entered. How do I prevent that?
[edited by: Champak at 5:35 pm (utc) on Mar. 19, 2007]
This is an optional parameter. If it is not included the following assumptions are made:
HTH
1/ So I understand, what is happening here, What is the 10 for? Just for argument sake, what if I used a 7?
As BlobFisk mentioned, this is the radix value. Though you *might* be able to leave this out, it's always safer to include it (as base 10 if you're using normal numbers). Also, if the value begins with "0x", it will try to parse it as a hexadecimal number. If it begins with "0", it parses the number in octal. Otherwise, it parses it as decimal.
2/ When entering the numbers in the inputbox for calculation, after I enter the first number, this throws up "NaN" in the result inputbox until the other number is entered. How do I prevent that?
Add some validation to make sure the user has entered a value.
var p = parseInt(ff_getElementByName('bor_base_income_data').value,10);
var q = parseInt(ff_getElementByName('co_bor_base_income_data').value, 10);
p = ( isNaN(p)? 0 : p );
q = ( isNaN(q)? 0 : q );
var baseincome = p + q;
In this example, I've added a check so that if p = NaN (Not a Number), then it sets p to 0. Likewise, it does the same check on q. So with no values (or invalid value) entered, both will default to zero.
[edited by: Fotiman at 8:05 pm (utc) on Mar. 19, 2007]
Floating point precision also drives me nuts. My floating point thread of doom [webmasterworld.com].