Skip to main content
Inspiring
Feb 21, 2022 5:27 PM UTC
Answered

how to add or subtract from given calc field

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 is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Feb 22, 2022 5:10 PM UTC

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

14 replies

JR Boulay
Community Expert
Community Expert
Feb 22, 2022 8:24 AM UTC

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

Translate
Acrobate du PDF, InDesigner et Photoshopographe
Inspiring
Feb 22, 2022 2:17 PM UTC

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.

Translate
try67
Community Expert
Community Expert
Feb 22, 2022 2:27 PM UTC

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;
Translate
JR Boulay
Community Expert
Community Expert
Feb 21, 2022 6:10 PM UTC

You can use the simplified notation:

 

Translate
Acrobate du PDF, InDesigner et Photoshopographe
Inspiring
Feb 21, 2022 7:48 PM UTC

right so i want to do that without the 3rd box

Translate
try67
Community Expert
Community Expert
Feb 21, 2022 5:29 PM UTC

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

Translate
Inspiring
Feb 21, 2022 7:48 PM UTC

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?

 

Translate