Calculate Percentage Of Two Integer Fields

-Andrew-
Kilo Sage

Dear all,

I hope everyone had a great festive break!

I'm working on a solution to calculate the percentage of two Integer fields. Lets say;

Field1 = 1

Field2 = 10

As Field1 is only 1, it should return 90%. If Field1 is 2, it should return 80%, and so on.

Here's my script of what i've tried so far but without any joy

var percentage = 0;
percentage=(field2 + field1)*100;

//have also tried 
percentage=(field1 + field2)*100;

Does someone mind giving me a bit of guidance on this? I've searched the forums and can't find anything that works for my use case.

 

Thanks!

3 REPLIES 3

Marion de Groo1
Tera Guru

So Field 2 is the 100%, and you want to know the percentage that is left when you take off the Field 1 %. So it must be like this:

The full percentage is 100%

1% is Field2*0,01

The percentage field 1 represents is Field1 / (Field2*0,01)

The left over percentage will be 100-(Field1 / (Field2*0,01))

 

 

 

Thanks Marion, could you help me translate this into a script?

shloke04
Kilo Patron

Hi,

If you want to do this on a Client Side then please use the script below. This can be on Load or an On Change client Script:

var x = g_form.getValue('FIELDNAME').replace(/,/g,''));
var y = g_form.getValue('FIELDNAME').replace(/,/g,''));
var pr = parseFloat(x);
var pc =parseFloat(y);

var percentage = pr - pc;

alert(percentage);
var uu = percentage / pr;
alert(uu);
var percentagevalue = parseFloat(percentage * 100);
alert('percentage :' + percentagevalue);
g_form.setValue('FIELD_NAME', percentagevalue.toFixed(2));

If this need to be done a Server Side then instead of using g_form API you need to use server equivalent such as for example "current".

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke