3 Replies Latest reply on Oct 27, 2011 3:12 PM by George_Johnson

# Creating a custom calculation script limited to two decimals

This question is really broader than the title. Here's what I'm trying to accomplish for an order form:

Line 1: Number of products ordered (A).

User indicates how many products he wishes to order. The total number of products ordered adds up to A.

Line 2: Product Cost (C).

A * 4.95 (which is the cost per unit) is the product cost (C). I want C to be two decimals after the decimal point, so if four units are ordered, the cost is \$19.80, not 19.8 (or 19.8000000000001)

Line 3: Shipping (S).

If they order one unit, the shipping cost is \$2.50

If they order 2-5 units, the shipping cost is \$5

If they order 6-15 units, the shipping cost is \$10

Line 4: Overall cost.

C + S

Can anyone tell me how to accomplish this? And how do I learn how to do this on my own?

Acrobat 9.4.6, Macintosh

• ###### 1. Re: Creating a custom calculation script limited to two decimals

For line 2, the custom calculate script would be something like:

```// Get the number ordered, as a number
var num = +getField("A").value;

// Calculate this field value and round to two digits to the right of the decimal
event.value = util.printf("%.2f", num * 4.95);
```

Replace "A" with the actual name of the field.

For the shipping cost, it could be:

```// Get the number ordered, as a number
var num = +getField("A").value;

// Set the default shipping price
sp = 0;

// Determine shipping price based on the number of items ordered
if (num === 1) sp = 2.5;
if (num >=2 && num <= 5) sp = 5
if (num >=6 && num <= 15) sp = 10

// Set this field's value to the shipping cost
event.value = sp;
```

Note that this doesn't deal with numbers greater than 15, apart from setting it to 0.

There are a lot of good tutorials at acrobatusers.com, but you'll have to search around there a bit.

• ###### 2. Re: Creating a custom calculation script limited to two decimals

George, thank you so much. That was huge!

One more question: when I add the product and shipping costs together, I don't always get two digits after the decimal point. Can you tell me what to enter for the calculation script in order for cost+shipping to equal a number rounded to two decimals to the right of the decimal point? Would it be something like:

`event.value = util.printf("%.2f", "Product_cost" + "Shipping")`

Thanks.

• ###### 3. Re: Creating a custom calculation script limited to two decimals

It wold have to be something like:

`event.value = util.printf("%.2f", +getField("Product_cost").value + +getField("Shipping").value);`

But if you just want to change what gets displayed, you could set the Format of the field to Number and set the number of decimal places.