Turn string into number for numerical comparison

jmiskey
Kilo Sage

I have a Catalog Item that has a Single Line Text variable, asking the user to enter a cost value.  The values may be entered in any one of these forms:

123

123.45

$123.45

1234

$1234

1234.56

$1,234

1,234.56

$1,234.56

 

I have to do some numerical comparisons on this variable in Flow Designer, i.e. if the value is less than $1,000, do one thing, if it is greater than $1,000, do something else, etcetera (there are actually more than two levels, but I have just simplified it for this example).

 

I imagine that in order to do these numerical greater than/lesser than comparisons in Flow Designer, I will need to convert my Single Line Text variable to a numerical value.  Knowing all the different formats the entry may be in (shown above), how to I convert that entry into a valid number so I can perform those calculation comparisons on it in Flow Designer?

 

Thanks

2 ACCEPTED SOLUTIONS

J Siva
Tera Sage

Hi @jmiskey 
Try the below regex script to remove the non-numeric characters (except .) from the string.

var str = "<Input string>";

// Step 1: Remove any non-digit, non-comma, non-dot characters (like $)
str = str.replace(/[^\d.,]/g, '');

// Step 2: Remove commas
str = str.replace(/,/g, '');

// Step 3: Convert to number
var number = parseFloat(str);

gs.info("Extracted number: " + number); 

 

Output:

JSiva_0-1753960164171.png

JSiva_1-1753960180755.png
Note: Modufy the script as required in the flow to set the output variable value

Hope this helps.
Regards,
Siva

 

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@jmiskey 

why not add a number validation at variable level itself and ask user to enter only number?

Then no need to do any manipulation in flow

You can add this Variable Validation Regex to your variable

AnkurBawiskar_0-1753960274799.png

OR

If you want user to enter symbol as well while entering amount then you can use your OWN Regex and add it in variable validation Regex OR you can use onChange catalog client script and throw error if validation fails.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

J Siva
Tera Sage

Hi @jmiskey 
Try the below regex script to remove the non-numeric characters (except .) from the string.

var str = "<Input string>";

// Step 1: Remove any non-digit, non-comma, non-dot characters (like $)
str = str.replace(/[^\d.,]/g, '');

// Step 2: Remove commas
str = str.replace(/,/g, '');

// Step 3: Convert to number
var number = parseFloat(str);

gs.info("Extracted number: " + number); 

 

Output:

JSiva_0-1753960164171.png

JSiva_1-1753960180755.png
Note: Modufy the script as required in the flow to set the output variable value

Hope this helps.
Regards,
Siva

 

Ankur Bawiskar
Tera Patron
Tera Patron

@jmiskey 

why not add a number validation at variable level itself and ask user to enter only number?

Then no need to do any manipulation in flow

You can add this Variable Validation Regex to your variable

AnkurBawiskar_0-1753960274799.png

OR

If you want user to enter symbol as well while entering amount then you can use your OWN Regex and add it in variable validation Regex OR you can use onChange catalog client script and throw error if validation fails.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I really like the idea, except there is just one small issue.  The "Number" RegEx option does not allow decimals!

So it will accept entries like:

123

but not entries like:

123.45

 

I would try to come up with it myself, but I know nothing about creating RegEx statements.  What would the RegEx statement to ONLY allow numbers and decimal points look like?  It would be great if we could come up with one that REQUIRES that they enter exactly 2 decimals.  So the entries would have to look something like:

123.00

123.10

123.45

12345.67

 

I think I found it:

^[0-9]*\.[0-9]{2}$

seems to work!

 

Thanks to everyone for their help!