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

how to limit a calculation and have remainder in new field

Participant ,
Jun 04, 2016 Jun 04, 2016

Copy link to clipboard

Copied

how can I limit the total to 40. Balance to populate into remainder field. This is the script I have

var f = this.getField("Text13");

var g = this.getField("Text14");

var j = this.getField("Text15");

event.value = f.value + g.value + j.value;

TOPICS
Acrobat SDK and JavaScript , Windows

Views

899

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
Advocate ,
Jun 04, 2016 Jun 04, 2016

Copy link to clipboard

Copied

Assuming that the total can not get beyond 79, you could do this:

• Create a field for the remainder ("remainder")

• Create a hidden, read-only field as carrier of the calculation ("calc").

• Rename (if needed) the field for the result to "total"

• Add this script to Calculation event of the calc field:

var f = this.getField("Text13") ;

var g = this.getField("Text14") ;

var j = this.getField("Text15") ;

var sum = f.value*1 + g.value*1 + h.value*1 ;

this.getField("total").value = 40 * Math.floor(sum / 40) ;

this.getField("remainder").value = sum % 40 ;

And that should do it.

It might be recommended to use more reasonable field names for the summands, however.

Hope this can help.

Votes

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
Participant ,
Jun 04, 2016 Jun 04, 2016

Copy link to clipboard

Copied

Hello Max, thanks for the quick reply. The script you provided worked, (as you said up to 79). But, as you know with form creating and javascripting fields, things change constantly. And, in my case I've decided to change the fields to only one field. And, have the "total" field max out at 8 then carry the balance over to the "remainder" field. I tried to work with your script and modify it to 8 by changing 40 to 8 and I wasn't successful. Can you guide me as to what to change and get your script to work with my changes?

This is how I changed the fields:

var f = this.getField("Text14") ;

var sum = f.value*1 ;

this.getField("total").value = 8 * Math.floor(sum / 8) ;

this.getField("remainder").value = sum % 8 ;

This kinda works up to 16. If I enter 17 into Text14 then 16 goes into the "total" field and 1 goes into "remainder".  That's not how I'd like it to work. If I enter 16 into "Text14" (or any other number higher) then 8 should go into the "total" and the balance should go into "remainder". Like this, 8 (into "total" and 8 into "remainder" and so on.

I really don't understand how the script works and combined with my lack of javascript knowledge I cannot figure it out. I'm purely experimenting at this point. Would you mind shedding a bit more light on the path to the solution? Thanks Max.

Votes

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
Advocate ,
Jun 05, 2016 Jun 05, 2016

Copy link to clipboard

Copied

OK, time for another exercise, this time as a function…

function somethingAndTheRest(inval, limit)

{

var a = math.min (inval, limit) ;

if (a > limit) {

return [limit, a - limit] ;

} else {

return [a, 0] ;

}

You call the function in this way (assuming you are going to look at the sum variable, an want it to be no greater than 8):

var satr = somethingAndTheRest(sum, 8) ;

this.getField("total").value = satr[0] ;

this.getField("remainder").value = satr[1] ;

Note that I have not tested the code, and therefore, it may have bugs.

Hope this can help.

Votes

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
Participant ,
Jun 05, 2016 Jun 05, 2016

Copy link to clipboard

Copied

Thanks for the "exercise". I'm always up to learning. But, I'm not sure I know where this new bit of script should be placed. I've tried my best placing it like so,

document level:

function somethingAndTheRest(inval, limit)

{

var a = math.min (inval, limit) ;

if (a > limit) {

return [limit, a - limit] ;

} else {

return [a, 0] ;

}

and this in custom calculation:

var satr = somethingAndTheRest(sum, 8) ;

this.getField("total").value = satr[0] ;

this.getField("remainder").value = satr[1] ;

am I getting warmer? A little hint would help? Thanks, Max.

Votes

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
Advocate ,
Jun 05, 2016 Jun 05, 2016

Copy link to clipboard

Copied

Indeed, you are getting closer…

You will, however, have to define the variable sum in the Calculate event before you can run these three lines.

Thin it actually should work…

Votes

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
Participant ,
Jun 05, 2016 Jun 05, 2016

Copy link to clipboard

Copied

If I knew what the variable sum looked like, I would be more than happy to define it. Sometimes learning isn't all the fun they say it is. I just dunno.

Votes

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
Advocate ,
Jun 05, 2016 Jun 05, 2016

Copy link to clipboard

Copied

LATEST

Well, you can see two definitions for the sum variable in this thread; how it will look in a particular case, I can't know; you would, however (because you know what the form should do at that particular place).

Votes

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