8 Replies Latest reply on Jul 31, 2013 1:40 PM by gkaiseril

# Calculating based on user input

Good afternoon - just learning Adobe javascripting by copy/paste/editing, but I cannot find something that I can use.

I have 4 inputs, 190 gal, 200 gal, 190 pt, 200 pt. The user selects how many of each item they want. I want to let them know the chargback amount based on their request.

var s1 = getField("190 gal").valueAsString;

var s2 = getField("200 gal").valueAsString;

var s3 = getField("190 pt").valueAsString;

var s4 = getField("200 pt").valueAsString;

I want the calcuation to show basically  ((s1+s2)*8.75)+((s3+s4)*35/24), printed as \$ with two decimal points.

Can anyone assist? Muchly appreciated.

• ###### 1. Re: Calculating based on user input

You're pretty much there... But what should happen if one or more of these are empty?

• ###### 2. Re: Calculating based on user input

There should be no issue. Most users will only want 1-2 of these items. They are not required to input values into any of the fields for items they dont need.

• ###### 3. Re: Calculating based on user input

I don't think you're following me... Unless you specifically tell the script not to execute if one of the fields is empty, it will treat it as zero, and the results might not be what you expect them to be.

• ###### 4. Re: Calculating based on user input

It would be fine if it counts any blanks as zero.

So using the 4 inputs, if someone entered:

1  x   190 gal

x   200 gal

3  x   190 pt

2  x   200pt

I would want the form to show them their cost as ((1+0)*8.75)+((3+2)*35/24) = \$16.04, and to have the total charge update each time they changed a number (not requiring them to click on a "calcuate total" button if at all possible)

• ###### 5. Re: Calculating based on user input

Use this calculation code, then:

var s1 = +this.getField("190 gal").valueAsString;

var s2 = +this.getField("200 gal").valueAsString;

var s3 = +this.getField("190 pt").valueAsString;

var s4 = +this.getField("200 pt").valueAsString;

event.value = ((s1+s2)*8.75)+((s3+s4)*35/24);

Set the Format of the field to Number with the currency symbol selected to have it automatically added.

• ###### 6. Re: Calculating based on user input

wow, thought there would be more to it! That worked perfectly. your help was greatly appreciated!

• ###### 7. Re: Calculating based on user input

You should not use valueAsString when adding field values unless you explicitly convert the strings to numbers (which is what try67 did with the "+" operator), though in this specific case it doesn't look like it will make a difference. But it's good not to get in the habit.

• ###### 8. Re: Calculating based on user input

I only use the 'valueAsString" property to test if a numeric field is a null string. Note that a null string is different than a zero value field.

You could also use the "Number" constrictor to convert number character strings to floating point numbers.

You should be aware that the JavaScript '+" operator is not only the addition operator but also is the string concatenation operator. How the '+' operator functions is dependent upon the type of values being processed. If both fields are of the type 'number' the values will be added. If either or both values are or the 'string' type then the values will concatenated.