# Thread: Syntax for mathematical formula

1. ## Syntax for mathematical formula

Hi

I have a range of values. Ive written the following formula. It does not seem to work. Can someone please tell me if ive got the operators and syntax right??

aMin_term_cost.value = ((aMonthly_Cost.value * aContract_Period.value) + aHandset_Cost.value ) - ((aMonthly_Cost.value * aMonths_Free.value) + ( aMonthly_Cost.value * (aMonths_Half_Price.value/2)) + aDiscount.value);
thanks

2. Why doesn't it work? Do you get undefined?
If so, maybe some of the references don't work because they don't exist and you'll get an error on fetching it's [code].value[/code property.

3. all the values exist but when you enter aDiscount.value or aHandset_Cost.value the wrong answer appears.

is it something to do with the + operator

if the answer is 120 when i enter a value for handset_cost ( eg 12)
the answer i get is 12012 . i.e the 12 is added on but not mathematically

if you don't enter the 2 values above the formula seems to work

4. Originally Posted by yass2006
...is it something to do with the + operator

if the answer is 120 when i enter a value for handset_cost ( eg 12)
the answer i get is 12012 . i.e the 12 is added on but not mathematically
Yes, I think it's to do with the addition operator. Those form.field.values that you are working with are still strings. The + operator will concatenate them as strings.
You need to change the form fields from strings to numbers before you perform your calculations.
I think you can make a numeric string into a number by subtracting zero from it.

"120" + 12 will equal 12012
("120" - 0) + 12 will equal 132

5. You can use the parseFloat() or parseInt() functions to convert a string to a float or integer.

6. Also Number() will convert strings to numbers.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•