4 Replies Latest reply on Mar 11, 2015 10:47 AM by helioc1227

# Showing negative numbers as 0

Box 1: User Input

Box 2: User Input

Box 3: User Input

Box 4: User Input

Box 5: (Box 1 - Box 2)

Box 6: (Box 3 + Box 4)

Box 7: (Box 5 - Box 6)

So I'm using the following equation for Boxes 5, 6 & 7:

var v1 = getField("Box #").valueAsString;

var v2 = getField("Box #").valueAsString;

if (v1 && v2) {

// Perform the calculation

event.value = +v1 - +v2;

} else {

event.value = "";

}

**Note for box 6, the in the calculation is replaced with (+).

------------------------------------------------------------------------------------------ --------------

My question: How do I write this code to produce/show "0" if the equation results in a negative number?

• ###### 1. Re: Showing negative numbers as 0

You need another "if" statement or you use a logical value to multiply the result of the calculation by 1 if not negative or by 0 if negative. The logical true and false have a numerical value. True is 1 and false is 0,

var v1 = this.getField("Box #").valueAsString;

var v2 = this.getField("Box #").valueAsString;

event.value = "";

if (v1 && v2) {

// Perform the calculation

if((v1 - 2) >= 0)  event.value = (v1 - 2) * ((v1 - 2) >= 0); // display result for non-negative values;

else event.value = 0; // display 0 for negative values;

or:

var v1 = this.getField("Box #").valueAsString;

var v2 = this.getField("Box #").valueAsString;

event.value = "";

if (v1 && v2) {

// Perform the calculation;

// display result if not negative or 0;

event.value = (v1 - 2) * ((v1 - 2) > 0);

}

Since the calculation is repeated with only a change in the field names, I would consider using a function that takes the field names as parameters and returns the result of the calculation.

// document level or page open function;

function PosativeResult(cField1, cField2, cOper) {

var v1 = this.getField(cField1).valueAsString;

var v2 = this.getField(cField2).valueAsString;

if(typeof cOper == "undefined") cOper = "+";

var nResult = "";

if (v1 && v2) {

// Perform the calculation;

nResult = eval(Number(v1) + " " + cOper + " " + Number(v2))

// display only positive results or zero;

nResult *= (nResult > 0);

}

return nResult;

} // end PosaiveResult function;

// custom calculation for Box5;

event.value = PosativeResult("Box1", "Box2", "-");

// custom calculation for Box6;'

event.value = PosativeResult("Box3", "Box4", "+");

// custom calculation for Box7;

event.value = PosativeResult("Box5", "Box6", "-");

Are you sure you provided the correct equations for the field values?

Changing the operator make the script more complex.

• ###### 2. Re: Showing negative numbers as 0

I'm definatley not an advanced user of Javascript, hence why I'm here.

Here's an example scenario and a little more understanding of what I'm trying to accomplish here:

Box 1: 5

Box 2: 10

Box 3: 20

Box 4: 0

Box 5: (Box 1 - Box 2) or (5 - 10) = -5

Box 6: (Box 3 + Box 4) or (20 + 0) = 20

Box 7: (Box 5 - Box 6) or ((-5) - (20)) = -25 = 0

The parameters need to allow the equation to work if anyone of these fields is blank or "0," also the field must also be hidden if all fields are blank.

• ###### 3. Re: Showing negative numbers as 0

Your example in the original post would have Box5 result as an empty value and not -5. Box7 would also display a blank field.

var v1 = getField("Box #").valueAsString;

var v2 = getField("Box #").valueAsString;

if (v1 && v2) {

// Perform the calculation

event.value = +v1 - +v2;

} else {

event.value = "";

}

Would only apply to Box 5 and Box7 because Box6 is not Box3 - Box4 but Box3 + Box4.

Since you want to display the results for Box5 and Box6 regardless of their values, you sample script would not apply.

You have also add the requirement for being field being hidden.

So the script for Box5 would be:

var v1 = this.getField("Box1").valueAsString;

var v2 = this.getField("Box2").valueAsString;

event.value = "";

if (v1 != "" && v2 != "") {

// Perform the calculation;

event.value = Number(v1) - Number(v2);

}

Box6 would be:

var v1 = this.getField("Box3").valueAsString;

var v2 = this.getField("Box4").valueAsString;

event.value = "";

if (v1 != "" && v2 != "") {

// Perform the calculation;

event.value = Number(v1) + Number(v2);

}

And Box7 would be:

var v1 = this.getField("Box5").valueAsString;

var v2 = this.getField("Box6").valueAsString;

if (v1 != "" && v2 != "") {

// Perform the calculation;

event.value = Number(v1) - Number(v2);

event.value *= (event.value > 0);

}

• ###### 4. Re: Showing negative numbers as 0

Yes, that is exactly what I need. Thanks!