Highlighted

## Convert a number field to negative and then multiple by another field

Community Beginner ,
Oct 20, 2020

Copied

Hello,

I'm new to using JavaScript (no training at all) and am trying to take a field that the user enters as a negative number and convert that number to a positive and then multiple by another field.

This is what I wrote, but it is not working correctly so I think it has to do with the order.

The first thing I'm trying to is to multiple by -1 to turn it to a positive number and then multiple by the other field.

``event.value = this.getfield("*S7 Special Functions Costs").value *-1 * this.getfield ("S5Total Meals");``

The first thing I'm trying to is to multiple the field "*S7 Special Functions Costs" by -1 to turn it to a positive number and then multiple by the other field "S5Total Meals".  I'm using the this.getfield because my field names have spaces and I used the '*' in my field names to denote fields that were used in other spots, which made sense when I started since its a multiple page Projected Operating Costs and that was before I started doing math in the form.

If any JavaScript expert can help I would appreciate it.

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

38

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

## Convert a number field to negative and then multiple by another field

Community Beginner ,
Oct 20, 2020

Copied

Hello,

I'm new to using JavaScript (no training at all) and am trying to take a field that the user enters as a negative number and convert that number to a positive and then multiple by another field.

This is what I wrote, but it is not working correctly so I think it has to do with the order.

The first thing I'm trying to is to multiple by -1 to turn it to a positive number and then multiple by the other field.

``event.value = this.getfield("*S7 Special Functions Costs").value *-1 * this.getfield ("S5Total Meals");``

The first thing I'm trying to is to multiple the field "*S7 Special Functions Costs" by -1 to turn it to a positive number and then multiple by the other field "S5Total Meals".  I'm using the this.getfield because my field names have spaces and I used the '*' in my field names to denote fields that were used in other spots, which made sense when I started since its a multiple page Projected Operating Costs and that was before I started doing math in the form.

If any JavaScript expert can help I would appreciate it.

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

39

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 20, 2020 0
5 Replies 5
Most Valuable Participant ,
Oct 20, 2020

Copied

You have a couple of errors there... Try this:

event.value = Number(this.getField("*S7 Special Functions Costs").value) * -1 * Number(this.getField("S5Total Meals").value);

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 20, 2020 0
Oct 20, 2020

Copied

If there's any possibility of the user (accidentally?) entering a positive number, or nothing at all in either of the two input fields, you might want to add additional code to deal with that.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 20, 2020 0
Most Valuable Participant ,
Oct 21, 2020

Copied

If either field is empty the code I posted above will just result in zero.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 21, 2020 0
Community Beginner ,
Oct 21, 2020

Copied

So I have another issue that is messing with the JavaScript.  The Field ("S5Total Meals") is a sum of several fields one of which has decimal places.  I don't want it to use the decimals.  So I search and found what to use.  So can you tell me where in this Script I would add the .toFixed (0) so that the end result doesn't have decimal places.

``````var nRate = this.getField("S5MealequivalentRate").value;

if(nRate !=0){event.value = this.getField("S5totalotherrev").value / nRate;} else {event.value = "";}``````

I think I might need extra "(" or ")" but again I'm learning as I go.  I tried adding it right after nRate but that took the decimal places from nRate which I don't want it to do.

I really appreciate the help

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 21, 2020 0
LATEST
Engaged ,
Oct 21, 2020

Copied

Try like this:  (this.getField("S5totalotherrev").value / nRate).toFixed(0)

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 21, 2020 0
Resources