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

Ankur,

 

While I like your alternative solution (using a RegEx command), there is still one lingering issue with it that needs to be worked out.  How do I convert the value into a number to be used in my Flow? 

 

I am trying to create an IF statement that says "if the value is greater than 1000, then proceed...".  However, if I choose the variable in an IF action in my Flow, since it is a Single Line Text variable, the only operators available to me are these text options:

jmiskey_0-1753962975131.png

 

Obviously, I need numeric operators like "greater than" and "less than".  What is the easiest way to convert this entry to a numeric value in my flow so I can create my IF statement?

I actually found my answer in another thread you replied to: https://www.servicenow.com/community/developer-forum/condition-in-flow-design/m-p/2590131

 

I just need to use a Flow Variable, and set the Flow Variable equal to the value from my Single Line Text Variable.

 

I think I have everything I need now.

jmiskey
Kilo Sage

I accepted J Siva's reply as the solution, as that is the one that actually answered the original question asked (even though I like the alternative suggestion better and ended up going with that).

@jmiskey 

I believe I also shared a valid answer and a better approach.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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

That is a nice new feature!  I did not realize that existed.  I have added yours to the accepted solutions.