Copy link to clipboard
Copied
Hello,
I know there are a number of posts about this but I still don't know what I'm doing wrong. FYI I am very new to Javascript, so I just need some actual help with the formula. What I need is when the result of an equation is negative, then the number turns to zero.
For example, MLA is the text box I am using to calculate the formula. The simplified formula I had was (NA/12). However that number is often times negative. So I tried to do a javacript with an if statement and it just wasn't working. Can anyone help me with this? THANK YOU!
Copy link to clipboard
Copied
Hi, you can use something like this:
if(event.value < 0){
event.value = 0;
}
Copy link to clipboard
Copied
Thank you so much for your help! Okay sorry to sound totally ignorant but do I type is just like you have it there? I have tried to do something like that and it isn't working. Also I need to type in the calculation before so would I do:
(NA/12)
if(event.value<0)
{event.value=0;}
like that? and I put that in the customized formula table, correct? Thanks again!
Copy link to clipboard
Copied
You have any other fields? what exactly are you calculating?
Copy link to clipboard
Copied
Yes there are a number of other fields prior to this that work fine because I can use the simple equations. So essentially text box NA is the last number that needs to be calculated and divided by 12 for text box MLA. With simple calculations it works fine, but when NA is a negative number that's where I run into problems. So if NA is negative, when calculated by the MLA box, the final number is negative. So I'm just trying to figure out how to continue to do the calculation of division in MLA, PLUS automatically change to zero if the calculated final nubmer is negative. Sorry I know that is probably a really easy answer I just can't figure it out.
Thanks a bunch for your help!
Copy link to clipboard
Copied
Try using this in "MLA" as custom calculation script:
var a = this.getField("NA").value;
event.value = a/12;
if(event.value < 0){
event.value = 0;
}
Copy link to clipboard
Copied
It worked! thank you so so much!
Copy link to clipboard
Copied
Use this code as the field's custom calculation script:
event.value = Math.max(0, Number(this.getField("NA").valueAsString)/12);