Skip to main content
Participant
September 10, 2024
Answered

NaN error

  • September 10, 2024
  • 2 replies
  • 655 views

I have the following simple script in Custom calculation script in a Text Field called AS QUOTED CM%.

When the PDF Form has yet to be populated the AS QUOTED PROFIT field is $0 & the AS QUOTED TOTAL field is blank which creates a NaN answer. I want the calculated As QUOTED CM% field to be formated for Percent but I always get an error during use which will confuse the users.

How can I create an if NaN then 0 statement which I believe will resolve ther error

 

event.value =
(this.getField("AS QUOTED PROFIT").value/this.getField("AS QUOTED TOTAL").value)
;

 

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer PDF Automation Station

Dividing by zero returns Infinity.  When a field formatted as a number returns something other than a number it will display NaN ("Not a Number").  If you add the following script to the end of your script it will not only elimate the NaN display, but it will allow you to format the field as a percent and avoid popup "The value entered does not match the format..." errors:

if(event.value=="Infinity" || event.value=="NaN"){event.value=""}

2 replies

PDF Automation Station
Community Expert
Community Expert
September 10, 2024

Dividing by zero returns Infinity.  When a field formatted as a number returns something other than a number it will display NaN ("Not a Number").  If you add the following script to the end of your script it will not only elimate the NaN display, but it will allow you to format the field as a percent and avoid popup "The value entered does not match the format..." errors:

if(event.value=="Infinity" || event.value=="NaN"){event.value=""}

Nesa Nurani
Community Expert
Community Expert
September 10, 2024

Check that "AS QUOTED TOTAL" is not 0 because you can't divide by zero, something like this:

if(this.getField("AS QUOTED TOTAL").value != 0)
event.value =
(this.getField("AS QUOTED PROFIT").value/this.getField("AS QUOTED TOTAL").value)
;