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

calculation involving multiplication, with a max range and rounding of final value

Community Beginner ,
Jan 24, 2020 Jan 24, 2020

Copy link to clipboard

Copied

Hope someone can help me with this. I think I understand how to do the individual action but can't seem to put them all togther to work.

I have 2 values ( A and B) that I need to multiply. This is easily done. I then need to validate that the answer does not exceed a max number (example 500). if it is over the max dose, the answer will always be 500. if the value is below 500, the number needs to be rounded to the nearest 50.

 

TOPICS
Acrobat SDK and JavaScript

Views

257

Likes

Translate

Translate

Report

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

correct answers 4 Correct answers

Community Expert , Jan 24, 2020 Jan 24, 2020

MODIFIED REPLY

 

Hi,

 

Assuming that you already have two form fields named A and B, you can try adding the following script to the custom calculation script of your total form field and see if it works for you:

 

 

 

//Add this script as custom calculation script of your total field

if(!event.willCommit)
console.println(!event.value);

var a = this.getField("A").value;
var b = this.getField("B").value;
var c = 50;
var d = 500;
if (a && b !="") event.value = a*b;
event.value = a*b;
if ( (a =="") && (b 
...

Likes

Translate

Translate
Community Expert , Jan 24, 2020 Jan 24, 2020

Thank you for spotting that. My mistake.

 

This is the correction:

 

if(!event.willCommit)
console.println(!event.value);

var a = this.getField("A").value;
var b = this.getField("B").value;
var c = 500;
if (a && b !="") event.value = a*b;
event.value = a*b;
if ( (a =="") && (b > 1) ) event.value = "";
if ( (a>1) && (b =="") ) event.value = "";
if (a*b > c) event.value = c;
if (a*b < c) event.value = Math.ceil((event.value/50))*50;

//Or you can use
if (a*b < c) event.value = Math.round((event.value
...

Likes

Translate

Translate
Community Expert , Jan 25, 2020 Jan 25, 2020

Another way of doing it is with the min method of the Math object, like so:

 

var a = Number(this.getField("A").valueAsString);
var b = Number(this.getField("B").valueAsString);
var v1 = (Math.round((a*b)/50))*50;
event.value = Math.min(v1, 500);

 

You didn't specify if you want to round the number down or up (or to the nearest) 50 multiple, so I used the latter.

If you want to round up replace Math.round with Math.ceil, and if you want to round down replace it with Math.floor in the third line

...

Likes

Translate

Translate
Community Beginner , Jan 25, 2020 Jan 25, 2020

Thanks everyone! 

Here is what I came up with base on your replies. This is actually a medication calculation on a PDF form. When a checkbox is ticked off, the calculation occurs. Works perfectly! 

 

 

if(this.getField("Rituximab").value=="Off")

{
event.value="";
}
else
{
var bsa_val = this.getField("BSA").value * 375
if(bsa_val > 800){
event.value = 800
}
else
{
event.value = Math.round(bsa_val / 50) * 50;
}
}

 

 

Likes

Translate

Translate
Community Expert ,
Jan 24, 2020 Jan 24, 2020

Copy link to clipboard

Copied

MODIFIED REPLY

 

Hi,

 

Assuming that you already have two form fields named A and B, you can try adding the following script to the custom calculation script of your total form field and see if it works for you:

 

 

 

//Add this script as custom calculation script of your total field

if(!event.willCommit)
console.println(!event.value);

var a = this.getField("A").value;
var b = this.getField("B").value;
var c = 50;
var d = 500;
if (a && b !="") event.value = a*b;
event.value = a*b;
if ( (a =="") && (b > 1) ) event.value = "";
if ( (a>1) && (b =="") ) event.value = "";
if (a*b >500) event.value = d;
if (a*b < c) event.value = Math.ceil((event.value/50))*50;

//Or you can use
if (a*b < c) event.value = Math.round((event.value/50))*50;

 

 

 

Likes

Translate

Translate

Report

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
Community Expert ,
Jan 24, 2020 Jan 24, 2020

Copy link to clipboard

Copied

Why the variable c ?

Likes

Translate

Translate

Report

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
Community Expert ,
Jan 24, 2020 Jan 24, 2020

Copy link to clipboard

Copied

Thank you for spotting that. My mistake.

 

This is the correction:

 

if(!event.willCommit)
console.println(!event.value);

var a = this.getField("A").value;
var b = this.getField("B").value;
var c = 500;
if (a && b !="") event.value = a*b;
event.value = a*b;
if ( (a =="") && (b > 1) ) event.value = "";
if ( (a>1) && (b =="") ) event.value = "";
if (a*b > c) event.value = c;
if (a*b < c) event.value = Math.ceil((event.value/50))*50;

//Or you can use
if (a*b < c) event.value = Math.round((event.value/50))*50;

Likes

Translate

Translate

Report

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
Community Expert ,
Jan 25, 2020 Jan 25, 2020

Copy link to clipboard

Copied

Another way of doing it is with the min method of the Math object, like so:

 

var a = Number(this.getField("A").valueAsString);
var b = Number(this.getField("B").valueAsString);
var v1 = (Math.round((a*b)/50))*50;
event.value = Math.min(v1, 500);

 

You didn't specify if you want to round the number down or up (or to the nearest) 50 multiple, so I used the latter.

If you want to round up replace Math.round with Math.ceil, and if you want to round down replace it with Math.floor in the third line of code above.

Likes

Translate

Translate

Report

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
Community Expert ,
Jan 25, 2020 Jan 25, 2020

Copy link to clipboard

Copied

Awesome! thanks.

Likes

Translate

Translate

Report

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
Community Beginner ,
Jan 25, 2020 Jan 25, 2020

Copy link to clipboard

Copied

LATEST

Thanks everyone! 

Here is what I came up with base on your replies. This is actually a medication calculation on a PDF form. When a checkbox is ticked off, the calculation occurs. Works perfectly! 

 

 

if(this.getField("Rituximab").value=="Off")

{
event.value="";
}
else
{
var bsa_val = this.getField("BSA").value * 375
if(bsa_val > 800){
event.value = 800
}
else
{
event.value = Math.round(bsa_val / 50) * 50;
}
}

 

 

Likes

Translate

Translate

Report

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