Click to See Complete Forum and Search --> : Adding numeric values together


stualk
11-10-2003, 06:02 AM
I have a slight problem. I have a form where I have successfully managed to make sure the fields at the end of each row in the form multiply the contents of the first field by the second, the result is displayed in the third when pressing the tab key. (i.e. 1st Field = 2, 2nd Field = 30, Result in 3rd = 60)

There are then 8 rows that repeat this process, at the bottom in the 9th I need to add together the results at the end of each row. I thought this would be simple but what I'm actually getting is a string result. For example, if the result on row 1 is 10 and the result on row 2 is 40, it returns 1040 at the bottom, rather than 50.

Can anyone help?

Cheers

Charles
11-10-2003, 06:10 AM
It's unfortunate, but the additive operator concatenates strings in JavaScript. The solution is to cast the values as numbers before adding them.

<string type="text/javascript">
<!--
alert ('1' + '2') // gives you "12"

alert (Number('1') + Number('2')) // gives you "3"
// -->
</script>

stualk
11-10-2003, 06:28 AM
What code do I actually need to add before the rest of my JavaScript then? I tried adding the code you used and it returned errors. Is there something that I can use that I can put in the first line of my Java code and solve the problem straight away?

Thanks for the help

Charles
11-10-2003, 06:38 AM
Originally posted by stualk
Is there something that I can use that I can put in the first line of my Java code and solve the problem straight away?No, every time you use the additive operator with strings, but you want the strings first converted to Numbers, you need to cast each string as a Number. If some property or variable, n, is a string then Number(n) is, oddly enough, a Number.

Khalid Ali
11-10-2003, 06:41 AM
Originally posted by Charles
It's unfortunate...


I don't think so...;)

As a matter of fact this practice of programming language to figure out by them seleves if a user is adding or concatenating should be dropped,One should always declare a type of variable they use,(hopefully in JS 2.0 we will be able to declare most data types), that will reduce the potential of any un-seen errors at run time..but thats just me..

stualk
11-10-2003, 07:31 AM
Ok, can anyone point me in the right direction here as I'm non the wiser? I understand the comments but I need a quick solution to my problem.

This is an example of the code I've used:

<script language="JavaScript">

function doRound(x, places) {
return Math.round(x * Math.pow(10, places)) / Math.pow(10, places);
}

function updateAdditionalCosts() {
var AdditionalCostsNos = document.forms["add"].additional_costs_nos_new.value;
var AdditionalCostsPer = document.forms["add"].additional_costs_per_new.value;
var grossAdditionalCostsValue = document.forms["add"].additional_costs_cost_new.value;

if (AdditionalCostsNos > 0) {
grossAdditionalCostsValue = AdditionalCostsNos * AdditionalCostsPer;
}

document.forms["add"].additional_costs_cost_new.value = doRound(grossAdditionalCostsValue, 4);
}

function updateCost() {
var RateValue = document.forms["add"].instructor_rate_cost_new.value;
var MileageValue = document.forms["add"].mileage_cost_new.value;
var AccomodationValue = document.forms["add"].accomodation_cost_new.value;
var DelegateValue = document.forms["add"].delegate_cost_cost_new.value;
var RegCardCostsValue = document.forms["add"].reg_card_costs_cost_new.value;
var OperatorsCardCostsValue = document.forms["add"].operators_card_costs_cost_new.value;
var SafetyTestsCostsValue = document.forms["add"].safety_tests_costs_cost_new.value;
var AdditionalCostsValue = document.forms["add"].additional_costs_cost_new.value;
var grossCostValue = document.forms["add"].cost_new.value;

if (RateValue > 0) {
grossCostValue = RateValue + MileageValue;
}

document.forms["add"].cost_new.value = doRound(grossCostValue, 4);
}
</script>

Can anyone suggest why the last part, which is meant to add two values together doesn't actually work, in coding terms? I'm still a novice!

Cheers for the help.

stualk
11-10-2003, 08:25 AM
Can anyone please help me? I'm totally stuck and desperate!

Cheers

Khalid Ali
11-10-2003, 08:48 AM
I think this willdo


var RateValue = Number(document.forms["add"].instructor_rate_cost_new.value);
var MileageValue = Number(document.forms["add"].mileage_cost_new.value);

stualk
11-10-2003, 09:35 AM
That works perfectly.

Thank you very much for your help. Much appreciated. I'm learning fast! :)

ChelseaCat
08-26-2007, 04:29 PM
I'm attempting to do something very similar (add numerical values) and found the posts above very helpful. But how could you link the function updateCost() from an onclick event handler? For example, I've created a form with numeric values and want to calculate them with a Calculate button. This doesn't work....

<INPUT type="button" name="Calculate" value="Calculate Profit"
onclick="function calculate()">

What is the proper way to do this? I've also tried it without the quotes around function calculate()

Thank you so much for any help.

:confused:

JMRKER
08-26-2007, 10:16 PM
This assumes you have created locations to hold the 'a', 'b' and 'c' values.

In the BODY section

<input type="text" id='a' value="3">
<input type="text" id='b' value="4">
<input type="text" id='c' value="">
<button onclick="Calculate()">Calculate</button>

In the HEAD section

function Calculate() {
var a = document.getElementById('a').value; a = Number(a);
var b = document.getElementById('b').value; b = Number(b);
var c = Math.sqrt((a*a) + (b*b));
docment.getElementById('c').value = c;
}

Should give an example of Pythagora's Theorum.
Modify example to match your particular needs.

felgall
08-26-2007, 11:47 PM
There are also shorter ways of converting a string into a number. For example:

a - 0
b * 1
c / 1