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

Changing negative value to 0

New Here ,
Aug 21, 2020 Aug 21, 2020

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!

TOPICS
Acrobat SDK and JavaScript
1.7K
Translate
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
Community Expert ,
Aug 21, 2020 Aug 21, 2020

Hi, you can use something like this:

if(event.value < 0){
event.value = 0;
}

Translate
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
New Here ,
Aug 21, 2020 Aug 21, 2020

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!

Translate
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
Enthusiast ,
Aug 21, 2020 Aug 21, 2020

You have any other fields? what exactly are you calculating?

Translate
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
New Here ,
Aug 21, 2020 Aug 21, 2020

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!

Translate
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
Community Expert ,
Aug 21, 2020 Aug 21, 2020

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

Translate
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
New Here ,
Aug 21, 2020 Aug 21, 2020
LATEST

It worked! thank you so so much!

Translate
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
Community Expert ,
Aug 21, 2020 Aug 21, 2020

Use this code as the field's custom calculation script:

 

event.value = Math.max(0, Number(this.getField("NA").valueAsString)/12);

Translate
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