How to perform math functions in a script include

matthew_magee1
Giga Guru

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-

1 ACCEPTED SOLUTION

matthew_magee1
Giga Guru

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

View solution in original post

11 REPLIES 11

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

DScroggins
Kilo Sage
Hi Matthew, Currency fields are stored as strings. So in order to perform math functions you first need to convert them to numbers.

John Longhitano
Kilo Guru

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.

It's that dang dollar sign that is coming over. You'd think there would be an easy way to strip that out:

find_real_file.png

DScroggins
Kilo Sage

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.