The CreatorCon Call for Content is officially open! Get started here.

How to convert variable 'single line text' in an integer?

davide_fais
Tera Expert

Hi ServiceNow Community,

I've a variable type 'single line text' and I'd like convert this in a integer value, for exampe:

var number=g_form.getValue('u_variable_number');

if (number > 10)

number ++

etc

How can I do this?

Thanks,

Best Regards.

1 ACCEPTED SOLUTION

drjohnchun
Tera Guru

If you decide to use the parseInt() function, best practice is to specify the second parameter for radix, which is 10 for the decimal system. You might end up with unexpected results if you omit it, as it says in parseInt() - JavaScript | MDN:



If radix is undefined or 0 (or absent), JavaScript assumes the following:


  • If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.
  • If the input string begins with "0", radix is eight (octal) or 10 (decimal).   Exactly which radix is chosen is implementation-dependent.   ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet.   For this reason always specify a radix when using parseInt.
  • If the input string begins with any other value, the radix is 10 (decimal).

If the first character cannot be converted to a number, parseInt returns NaN.



Hope this helps.



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile

visit snowaid


ServiceNow Advocate

View solution in original post

5 REPLIES 5

drjohnchun
Tera Guru

If you decide to use the parseInt() function, best practice is to specify the second parameter for radix, which is 10 for the decimal system. You might end up with unexpected results if you omit it, as it says in parseInt() - JavaScript | MDN:



If radix is undefined or 0 (or absent), JavaScript assumes the following:


  • If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.
  • If the input string begins with "0", radix is eight (octal) or 10 (decimal).   Exactly which radix is chosen is implementation-dependent.   ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet.   For this reason always specify a radix when using parseInt.
  • If the input string begins with any other value, the radix is 10 (decimal).

If the first character cannot be converted to a number, parseInt returns NaN.



Hope this helps.



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile

visit snowaid


ServiceNow Advocate