LiveCycle Designer: parseInt parses numbers based on a base/radix


If you are using the parseInt() method in Javascript, you may receive some unexpected results. This is especially true if you are using any kind of prefixes in the value.

For example:

parseInt(“09”) would return “0”

and parseInt(“045”) would return “55”


Use parseFloat() to get expected results for numbers that are prefixed with “0”.  Use parseInt(“09”, 10), where 10 specifies the base, that is, decimal.

Additional Information

The “0” prefix causes the unexpected results from parseInt(). Numbers with a “0” prefix are taken as octal (base 8 ) numbers as opposed to decimal numbers. As a result, the octal 55 is equal to a decimal 45.

parseInt syntax: parseInt('string' [, base])

How it works:

The first argument of parseInt must be a string or a string expression. The result returned by parseInt is an integer whose representation was contained in that string (or the integer found in the beginning of the string). The second argument (base), if present, specifies the base (radix) of the number whose string representation is contained in the string. The base argument can be any integer from 2 to 36.

If there is only one argument, the number base is detected according to the general JavaScript syntax for numbers. Strings that begin with 0x or -0x are parsed as hexadecimals; strings that begin with 0 or -0 are parsed as octal numbers. All other strings are parsed as decimal numbers.

If the string argument cannot be parsed as an integer, the results will be different in different interpreters (either 0 or NaN).

Examples (comments in each line give the conversion results):

parseInt('123.45') // 123
parseInt('77') // 77
parseInt('077',10) // 77
parseInt('77',8) // 63 (= 7 + 7*8)
parseInt('077') // 63 (= 7 + 7*8)
parseInt('77',16) // 119 (= 7 + 7*16)
parseInt('0x77') // 119 (= 7 + 7*16)
parseInt('099') // 0 (9 is not an octal digit)
parseInt('99',8) // 0 or NaN, depending on the platform
parseInt('0.1e6') // 0
parseInt('ZZ',36) // 1295 (= 35 + 35*36)

reference: (1-16936533)

VN:F [1.9.22_1171]
Was this helpful? Please rate the content.
Rating: 10.0/10 (1 vote cast)
LiveCycle Designer: parseInt parses numbers based on a base/radix, 10.0 out of 10 based on 1 rating

Comments are closed.