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

Harsh Vardhan
Giga Patron

use parseInt(diff)

 

eg: var diff = parseInt(actual/planned);

 

 https://www.w3schools.com/jsref/jsref_parseint.asp

Yep, tried that. Still no worky.

 

find_real_file.png

 

This is from w3 schools example

you have to remove the $ amount , 

var actual = 1600;



var planned= 1000;



var diff = parseInt(actual/planned);

gs.log('Diff: ' + diff);