
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2019 01:06 PM
We have a couple currency fields on a form that we want to do some math to set some other values.
For example, I have two fields: planned_cost and actual_cost.
I want to divide actual/planned to get a percentage and then I'll take that value to do something else.
I figure the 'best' way to do this is to perform a glideajax call to a script include to do the math and then return the answer back to client where I can some some setValues based on the percentage returned from the script include.
In my script include, I pull in the two values with this:
var actual = this.getParameter('sysparm_work_cost');
gs.log('Actual: ' + actual);
var planned= this.getParameter('sysparm_planned_cost');
gs.log('Planned: ' + planned);
var diff = (actual/planned);
gs.log('Diff: ' + diff);
When I view my logs, I get this:
Actual: $1,600
Planned: $1,000
Diff: NaN​
Is there a way to convert currency to a number? Should I be doing this math on the client script?
Any help is greatly appreciated-
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-24-2019 08:45 AM
Got it worked out. Thanks to everyone who chimed in:
var actual = parseFloat(this.getParameter('sysparm_work_cost').slice(1)); this converts the string $1,600.00 to 1,600.00
var planned= parseFloat(this.getParameter('sysparm_planned_cost').slice(1)); ; this converts the string $1,100.00 to 1,100.00
var diff = actual/planned;
gs.log('Diff: ' + diff);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-24-2019 08:44 AM
Yeah, the values are coming over from the form w/ the $ sign.
We figured it out:
var actual = parseFloat(this.getParameter('sysparm_work_cost').slice(1)); this converts the string $1,600.00 to 1,600.00
var planned= parseFloat(this.getParameter('sysparm_planned_cost').slice(1)); ; this converts the string $1,100.00 to 1,100.00
var diff = actual/planned;
gs.log('Diff: ' + diff);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2019 01:21 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2019 01:49 PM
Hi Matt,
You will probably have to use parseInt() as mentioned above.
Run the following in http://jsbin.com/?js,console to understand what is happening.
'15' is a string and math cannot be performed. 15 is an integer and math functions can be performed.
var numberString = '15';
var numberInt = parseInt(numberString, 10);
numberString = numberString + numberString;
numberInt = numberInt + numberInt;
console.log('Math on a String: ' + numberString);
console.log('Math on an Integer: ' + numberInt);
The log will read as follows:
"Math on a String: 1515"
"Math on an Integer: 30"
Hope that helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-24-2019 05:14 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-24-2019 06:55 AM
See the following on how to use currency values in scripts:
https://docs.servicenow.com/bundle/newyork-platform-administration/page/administer/currency/concept/currency-values-scripts.html
Use parseFloat() to get the value then you can perform the math functions.