7 Replies Latest reply on Jun 18, 2013 2:15 PM by gkaiseril

# subtract and divide in a single custom calc script

I am very new to all this and have created one custom script with help from this community.  I am trying to get the precentage of increase when a person gets a pay increase..

My fields are Pay1=old rate, Pay2= new rate, Pay3=increase amount and Pay4 precentage of increase.

I have the following script in the Pay4 fields:

var P1 = Number(this.getField("Pay1").value);

var P2 = Number(this.getField("Pay2").value);

if (P1 === 0) {P1 =1}

if (P2 === 0) {P2 =1}

var P3R = P1/P2*100;

var PERC3NT = 100 - P3R;

PERC3NT = PERC3NT / 100;

this.getField("Pay4").value = PERC3NT;

I noticed if I replace "Pay2" with "Pay3" i get a smaller amount as the precentage.  I know that in order to get the precentage i need to divide the difference of  the increase by the old rate and times by 100.  I seem to be missing something becasue even when I change the rate to one dollar more my precentage doesn't seem right.  Can you help.

• ###### 1. Re: subtract and divide in a single custom calc script

Is this the calculation script for Pay4? If so, what kind of value should it have? The percentage rise between the old and the new salaries?

So if the old salary was 80 and the new 90, it should be 12.5%?

• ###### 2. Re: subtract and divide in a single custom calc script

Yes, this is the script in Pay4.  We want the value to be a precentage of the new rate versus the old rate.  So if an employee received an increase from 80 to 90 it should be 12.5%.

With the script listed above it shows the precent as 11.1%

• ###### 3. Re: subtract and divide in a single custom calc script

That's because you need to divide the new salary by the old one, not the other way around.

I believe this code should work correctly:

var P1 = Number(this.getField("Pay1").value);

var P2 = Number(this.getField("Pay2").value);

if (P1==0 || P2==0) {event.value = ""}

else event.value = ((P2/P1)-1)*100;

• ###### 4. Re: subtract and divide in a single custom calc script

Thank you!!! this worked.  The only other question I had was now it shows as 1,250.00%.  I have my format set to precent and I chose 1 decimal place.  Is there anything I can add to the scrpit or change to make it as just the precent with no commas? Hope that make sense.

• ###### 5. Re: subtract and divide in a single custom calc script

Remove the part that multiplies it by 100 from the code...

The thousands separator can be set via the Format tab.

• ###### 6. Re: subtract and divide in a single custom calc script

Thanks again for all your help.

• ###### 7. Re: subtract and divide in a single custom calc script

Just compute the difference and divide by the starting pay value:

var P1 = Number(this.getField("Pay1").value); // starting base pay;

var P2 = Number(this.getField("Pay2").value); // ending pay;

event.value = 0; // clear the field

if(P1 != 0 & P2 != 0) {

// only compute if divisor is non-zero and there is an ending pay value;

event.value = (P2 - P1) / P1; // decimal change in pay rate from base pay;

}

Use parenthesis to set the calculation order.