8 Replies Latest reply on Jul 29, 2013 7:40 AM by hp600jake

# Need Help Creating Custom Calculation Script Please

Hello,

I am in desperate need of a custom script here if anyone knows how to do it. I essentially want the percentage field to automatically fill in based on the fraction, rounded to the nearest integer, show "0" if F2 is "0", and not display "NaN" before it's filled out. I also do not want the error message to pop up "The value entered does not match the format of the field [ P ]"

F1 and F2 will always be a whole number between 0-9, and P will always be a whole number between 0-100.

I've been using (F1/F2)*100 as a Simplified Field Notation, but it keeps showing decimals and displaying "NaN" when nothing has been entered in F1 and F2. Any gurus out there that could help me? I'd really appreciate it. Thanks

• ###### 1. Re: Need Help Creating Custom Calculation Script Please

I believe the follow does what you said you want:

// Custom Calculate script

(function () {

// Get the input field values, as strings

var s1 = getField("F1").valueAsString;

var s2 = getField("F2").valueAsString;

// Only proceed if there is a value in both fields

if (s1 && s2) {

// Convert field values to numbers

var v1 = +s1;

var v2 = +s2;

// Perform the division if the denominator is not zero

if (v2 !== 0) {

event.value = util.printf("%.0f", 100 * v1 / v2);

} else {

event.value = 0;

}

} else {

// Blank this field if either input is blank

event.value = "";

}

})();

• ###### 2. Re: Need Help Creating Custom Calculation Script Please

Thank you so much! Works perfectly.

• ###### 3. Re: Need Help Creating Custom Calculation Script Please

How do I do this same thing, but for a dollar amount?

• ###### 4. Re: Need Help Creating Custom Calculation Script Please

You should provide more details about the exact behavior you're looking for. Give an example of some input values and the desired output.

• ###### 5. Re: Need Help Creating Custom Calculation Script Please

Numerical example: 2,500.00/24=104.16  ( would like the total to be rounded down instead of up?)

Field example: Text121/Text125=Text129

• ###### 6. Re: Need Help Creating Custom Calculation Script Please

Replace that one similar line of code above with:

util.printf("%.2f", Math.floor(100*v1/v2)/100);

This will round towards negative infinity, as opposed to zero. So if it were calculating -2500/24, the result will be: -104.17

If this is not what you want, post again.

• ###### 7. Re: Need Help Creating Custom Calculation Script Please

That worked perfectly, thank you!

• ###### 8. Re: Need Help Creating Custom Calculation Script Please

Hello George,

You have been soo helpful to these folks maybe you can assist me with my issue.

Here is the formula I have than I will state the exceptions I am looking for.

DeductionsLine3 = DeductionsLine2-DeductionsLine1

These are dollar amounts so i would like them to:

A: Be rounded up the the nearst 100th which your above calculations did.

B. I need the number to not have the - infront of it which I completed with Math.abs

C. I need the DeductionsLine3 to show 0 for number that is returned as a negative. For example  10-20 =0

Your assistance would be greatly appreciated.