
- 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 07:52 AM
I realized that the above will not work in your use case. For what you are trying to accomplish using GlideAjax you first need to remove the '$' and the ',' from the string then you can convert to a number using parseFloat(). In your example the code would look like so:
var actual = this.getParameter('sysparm_work_cost');
var actualNum = parseFloat(actual.replace(/[^0-9\.-]+/g,""));
gs.log('Actual: ' + actualNum);
var planned = this.getParameter('sysparm_planned_cost');
var plannedNum = parseFloat(planned.replace(/[^0-9\.-]+/g,""));
gs.log('Planned: ' + plannedNum);
var diff = (actualNum/plannedNum);
gs.log('Diff: ' + diff);
NOTE: Use parseFloat() instead of parseInt() as your currency values my not always be whole numbers i.e. $1000 vs $1000.67
Hope this helps.
--David

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