Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Need help creating custom calculation script in Adobe Acrobat Pro

New Here ,
Sep 15, 2016 Sep 15, 2016

I am creating a fillable form for a college midterm grading rubric. I have the totals adding up, getting divided by 15 (number of questions) to give me my average. Then, I have a table with corresponding percentages based on what the average score is. I need to create a custom calculation script to do an if/then situation. "If your average score is 1.9, then your percent is 97%". However, when I divide the total by 15 I am often left with a number that has many decimals. I really don't want to have to go in and type in every possible decimal point in order for the percentage calculations to come out correctly. Is there a way to do a if/else if situation like "If average is between 1.7 and 1.79, then your percentage is 91%"?

Hopefully this makes sense and thank you in advance for any suggestions!

TOPICS
Acrobat SDK and JavaScript
554
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 15, 2016 Sep 15, 2016

You have to round the result. This can be done using the "Format" option of "Number" and specifying the number of zeros. This will round up. You could also force the value of the field to the rounded value using the "util.printf" method. This too will round up. You can use the Math.ceil and Math.floor methds to not round but go tot the nest larger value or truncate the value.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 15, 2016 Sep 15, 2016

Yes, there is a way to do that using an if/else if/else statement, no rounding needed. If you post your current script I can suggest a revision.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 15, 2016 Sep 15, 2016

Hi George,

This is the current script I'm working with. It originated on something that I was dividing by 10 instead of 15. That made the decimal count go down significantly. For example: if my Average_Score is 1.7222222222 I would need the output value to read as 91%.  Thank you!

var v = this.getField("Average_Score").valueAsString;

if (v=="2") event.value = "100%";

else if (v=="1.95") event.value = "100%";

else if (v=="1.9") event.value = "97%";

else if (v=="1.85") event.value = "97%";

else if (v=="1.8") event.value = "94%";

else if (v=="1.75") event.value = "94%";

else if (v=="1.7") event.value = "91%";

else if (v=="1.65") event.value = "91%";

else if (v=="1.6") event.value = "88%";

else if (v=="1.55") event.value = "88%";

else if (v=="1.5") event.value = "85%";

else if (v=="1.45") event.value = "85%";

else if (v=="1.4") event.value = "82%";

else if (v=="1.35") event.value = "82%";

else if (v=="1.3") event.value = "79%";

else if (v=="1.25") event.value = "79%";

else if (v=="1.2") event.value = "76%";

else if (v=="1.15") event.value = "76%";

else if (v=="1.1") event.value = "73%";

else if (v=="1.05") event.value = "73%";

else if (v=="1") event.value = "70%";

else if (v=="0.95") event.value = "70%";

else if (v=="0.9") event.value = "63%";

else if (v=="0.85") event.value = "63%";

else if (v=="0.8") event.value = "56%";

else if (v=="0.75") event.value = "56%";

else if (v=="0.7") event.value = "49%";

else if (v=="0.65") event.value = "49%";

else if (v=="0.6") event.value = "42%";

else if (v=="0.55") event.value = "42%";

else if (v=="0.5") event.value = "35%";

else if (v=="0.45") event.value = "35%";

else if (v=="0.4") event.value = "28%";

else if (v=="0.35") event.value = "28%";

else if (v=="0.3") event.value = "21%";

else if (v=="0.25") event.value = "21%";

else if (v=="0.2") event.value = "14%";

else if (v=="0.15") event.value = "14%";

else if (v=="0.1") event.value = "7%";

else if (v=="0.05") event.value = "7%";

else if (v=="0") event.value = "0%";

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 15, 2016 Sep 15, 2016
LATEST

Try something like this:

// Get the field value, as a number

var v = +getField("Average_Score").value;

// Set this field value based on the number

if (v >= 1.95) event.value = "100%";

else if (v >= 1.85 && v < 1.95) event.value = "97%";

else if (v >= 1.75 && v < 1.85) event.value = "94%";

// etc.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines