Skip to main content
Inspiring
February 21, 2022
Answered

how to add or subtract from given calc field

  • February 21, 2022
  • 3 replies
  • 2131 views

im trying to add or subtract numbers in a field by entering the numbers into the actual calculated field. exp, field box calc is "var1+var2=x" so lets say x= 90. i want to subtract 22 from the box with 90 by entering 22 into the box and it will display 68

This topic has been closed for replies.
Correct answer try67

i tried switching this to just add a number entered and it instead adds the number to the end of the previous number.


Try this:

 

if (event.value && event.target.value!="") event.value = Number(event.target.value)+Number(event.value);

3 replies

JR Boulay
Community Expert
Community Expert
February 22, 2022

Rather than saying what you don't want, you should say what you do want and how.
Please, elaborate your explanations.

Acrobate du PDF, InDesigner et Photoshopographe
Inspiring
February 22, 2022

This was close to what i want to do.

var a = this.getField("field1").value;
var f = event.value;

event.value = a;

if (a !=="" && f !=="") {event.value = a - f;}

if (a =="" && f !=="") {event.value ="";}

but instead of just subtracting the first number thats entered from the total of 120 i want it to continue subtracting from the previous calculation. so when you enter 11 into the field it will display 109. then enter 11 into the field again with 109 and it calculate 98.

try67
Community Expert
Community Expert
February 22, 2022

This is a very confusing way of doing things, but if you really want to do it, you can use this code as the custom Validation script of the second field:

 

if (event.target.value=="") event.value = Number(this.getField("field1").valueAsString)-event.value;
else event.value = Number(event.target.value)-event.value;
JR Boulay
Community Expert
Community Expert
February 21, 2022

You can use the simplified notation:

 

Acrobate du PDF, InDesigner et Photoshopographe
Participant
July 20, 2023

Thank you! I was thinking in a more complicated way and it wasn't working for me. You are awesome!

try67
Community Expert
Community Expert
February 21, 2022

So you want to use three fields for this, or just two?

Inspiring
February 21, 2022

2. so i have a total of 102 which is a set number. i want another field to display that number and to minus however much is input into the box that displays that number by entering the number into the same box. so it will display 102 when opened and say i want to minus 22 from that box. i want to just enter 22 into the box that says 102 and it to calculate it to 80. is that possible?