12 Replies Latest reply on Feb 6, 2014 5:37 AM by atravis42

# Custom Calculation Script for building in a percentage increase

Hello all,

Looking for a custom calculation script that will apply a percentage increase to a default value when the value of another cell goes above certain numbers. Crude example below.

Y is the cell that the script will be applied to.

if value of cell X > 10 then value of cell Y = default value * .10 ..... for 10%

if value of cell X > 20 then value of cell Y = default value * .20...... for 20%

if value of cell X > 30 then value of cell Y = default value * .30....... for 30%

Pleaes forgive me. I know my example above is not in true Java form because I really do not know Java that well. Thanks for any help that you all may provide!

• ###### 1. Re: Custom Calculation Script for building in a percentage increase

Where is this "default value" coming from? Is it a static number? The value of another field?

• ###### 2. Re: Custom Calculation Script for building in a percentage increase

Also, should this progression continue up to 100% (and possiblt beyond), or just until 30%?

• ###### 3. Re: Custom Calculation Script for building in a percentage increase

I've inserted the picture above to help. This is a sizing form for pants for a fire department that will be filled out at the department by the firefighter. The manufacturer that makes the pants passes along a 10% upcharge for anyone with a waist 44-50, a 20% upcharge for anyone with a waist 52-54, and a 30% upcharge for anyone with a waist 56-58. The \$42.95 in the "Price" cell is the default price for anyone with a waist that does not meet up-charging penalties (Less than a 44 waist). What I am trying to do is if the firefighter plugs in let's say a waist of 50" the "price' cell will update to show an increase of 10%. (\$47.24). If he plugs in a waist of 52" the "price" cell will update to show an increase of 20% (\$51.54). If he plugs in a waist of 56" the "price" cell will update to show an increase of 30% (\$55.83). I hope that makes sense. I know it can be frustrating helping someone that may not know all the ins and outs like a lot of you do so I greatly appreciate your patience and your help!

• ###### 4. Re: Custom Calculation Script for building in a percentage increase

Do you want to hard-code the "default" price into the code, or do you want to use for it something like the default value of the field?

• ###### 5. Re: Custom Calculation Script for building in a percentage increase

We can hard code the default price in that will be just fine. I have other rows that I am going to apply this code to so I will use this code as a foundation for the other ones. So if we build the default value into the code in that will help me with the other ones as well. Thanks again!

• ###### 6. Re: Custom Calculation Script for building in a percentage increase

You can use something like this as the custom calculation script of the Price field:

var basePrice = 42.95;

var waist = Number(this.getField("Waist").value);

if (waist>=44 && waist<=50)

basePrice *= 1.10; // 10% upcharge

else if (waist>=52 && waist<=54)

basePrice *= 1.20; // 20% upcharge

else if (waist>=56 && waist<=58)

basePrice *= 1.30; // 30% upcharge

event.value = basePrice;

One thing to notice, though, is that the Total field is calculated after the Price field, or you will get wrong results.

This can be set in Form Edit mode, via Tasks - Other Tasks - Set Field Calculation Order.

• ###### 7. Re: Custom Calculation Script for building in a percentage increase

Man... Thanks for doing all that. I had to step out away from the computer for the afternoon. I will plug that in tonight and give it a go. I will post back the results. Thanks again!

• ###### 8. Re: Custom Calculation Script for building in a percentage increase

Worked like a charm! Can't thank you enough! I have hit a bit of a roadblock again as I tried to modify the script for a different type of garment in a different row. They will need to enter a size such as L, 2XL, 3XL, 4XL... etc. I tried to enter the script below for it and got a syntax error. It seems as if it does not like the letters (XL) after the numbers. I'm sure it is a simple change, I'm just not having any luck being able to figure it out on my end. Thanks again!

var basePrice = 67.00;

var SizeJobshirt = Number(this.getField("SizeJobshirt").value);

if (SizeJobshirt=2xl)

basePrice *= 1.20; // 20% upcharge

else if (SizeJobshirt=3xl)

basePrice *= 1.30; // 30% upcharge

else if (SizeJobshirt=4xl)

basePrice *= 1.40; // 40% upcharge

else if (SizeJobshirt=5xl && 6xl)

basePrice *= 1.50; // 50% upcharge

event.value = basePrice; event.value = Math.floor(event.value * 100) / 100;

• ###### 9. Re: Custom Calculation Script for building in a percentage increase

You have quite a lot of problems in your code... Use this:

var basePrice = 67.00;

var SizeJobshirt = this.getField("SizeJobshirt").value;

if (SizeJobshirt=="2xl")

basePrice *= 1.20; // 20% upcharge

else if (SizeJobshirt=="3xl")

basePrice *= 1.30; // 30% upcharge

else if (SizeJobshirt=="4xl")

basePrice *= 1.40; // 40% upcharge

else if (SizeJobshirt=="5xl" || SizeJobshirt=="6xl")

basePrice *= 1.50; // 50% upcharge

event.value = Math.floor(basePrice * 100) / 100;

I'm assuming that "5xl" and "6xl" are separate items here, and that both should have a 50% upcharge.

• ###### 10. Re: Custom Calculation Script for building in a percentage increase

Haa, there are quite a few problems in my code because you know what you're doing and I do not!! Worked great. One thing I got figured out on the first script we did was how to keep the price from rounding up. My concern now is that if you take the base price of 67.00 and multiply it by .20 to get a 20% up-charge (additional \$13.40), my text box shows \$80.39. It should be \$80.40. I've tried a few things in the past few minutes but no luck. Any advice? I've attached a pic below to possibly help. Thanks again for everything by the way. I'm real close to being able to complete this form and certainly could not have gotten anywhere close to this without your help!

• ###### 11. Re: Custom Calculation Script for building in a percentage increase

This is caused by a generic bug in JavaScript that has to do with floating number calculations.

Try using Math.round instead of Math.floor ...

• ###### 12. Re: Custom Calculation Script for building in a percentage increase

Everything is working like a charm. I'm sure I've said it a hundred times already but THANKS!! Much appreciated!