Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

- Home
- Acrobat SDK
- Discussions
- calculation involving multiplication, with a max r...

- calculation involving multiplication, with a max r...

/t5/acrobat-sdk-discussions/calculation-involving-multiplication-with-a-max-range-and-rounding-of-final-value/td-p/10879361
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

Community guidelines

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

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
```

...
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
```

...
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

...
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;

}

}

6
Replies
6

/t5/acrobat-sdk-discussions/calculation-involving-multiplication-with-a-max-range-and-rounding-of-final-value/m-p/10879512#M8110
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;
```

Community guidelines

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

/t5/acrobat-sdk-discussions/calculation-involving-multiplication-with-a-max-range-and-rounding-of-final-value/m-p/10879571#M8111
Jan 24, 2020
Jan 24, 2020

Copy link to clipboard

Copied

Why the variable c ?

Community guidelines

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

/t5/acrobat-sdk-discussions/calculation-involving-multiplication-with-a-max-range-and-rounding-of-final-value/m-p/10879580#M8112
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;
```

Community guidelines

/t5/acrobat-sdk-discussions/calculation-involving-multiplication-with-a-max-range-and-rounding-of-final-value/m-p/10879663#M8113
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.

Community guidelines

/t5/acrobat-sdk-discussions/calculation-involving-multiplication-with-a-max-range-and-rounding-of-final-value/m-p/10879666#M8114
Jan 25, 2020
Jan 25, 2020

Copy link to clipboard

Copied

Awesome! thanks.

Community guidelines

Community Beginner
,

LATEST
/t5/acrobat-sdk-discussions/calculation-involving-multiplication-with-a-max-range-and-rounding-of-final-value/m-p/10880448#M8116
Jan 25, 2020
Jan 25, 2020

Copy link to clipboard

Copied

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;

}

}

Community guidelines

Copyright © 2023 Adobe. All rights reserved.