Copy link to clipboard
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.
Copy link to clipboard
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);
Copy link to clipboard
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.
Copy link to clipboard
Copied
If either field is empty the code I posted above will just result in zero.
Copy link to clipboard
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
Copy link to clipboard
Copied
Try like this: (this.getField("S5totalotherrev").value / nRate).toFixed(0)