Skip to main content

String-to-Number Conversion

String-to-Number Conversion

A string can be numeric, partially numeric, or non-numeric.

  • A numeric string consists entirely of numeric characters. For example, "123", "+123", ".123", "++0007", "-0".

  • A partially numeric string is a string that begins with numeric symbols, followed by non-numeric characters. For example, "3 blind mice", "-12 degrees".

  • A non-numeric string begins with a non-numeric character. For example, " 123", "the 3 blind mice", "three blind mice".

Numeric Strings

When a numeric string or partially numeric string is used in an arithmetic expression, it is interpreted as a number. This numeric value is obtained by scanning the string from left to right to find the longest sequence of leading characters that can be interpreted as a numeric literal. The following characters are permitted:

Note that the NumericGroupSeparator property value (the , character, by default) is not considered a numeric character. Therefore, the string "123,456" is a partially numeric string that resolves to the number "123".

Numeric strings and partial numeric strings are converted to canonical form prior to arithmetic operations (such as addition and subtraction) and greater than/less than comparison operations (<, >, <=, >=). Numeric strings are not converted to canonical form prior to equality comparisons (=, '=), because these operators are also used for string comparisons.

The following example shows arithmetic comparisons of numeric strings:

  WRITE "3" + 4,!             // returns 7
  WRITE "003.0" + 4,!         // returns 7
  WRITE "++--3" + 4,!         // returns 7
  WRITE "3 blind mice" + 4,!  // returns 7

The following example shows less than (<) comparisons of numeric strings:

  WRITE "3" < 4,!             // returns 1
  WRITE "003.0" < 4,!         // returns 1
  WRITE "++--3" < 4,!         // returns 1
  WRITE "3 blind mice" < 4,!  // returns 1

The following example shows <= comparisons of numeric strings:

  WRITE "4" <= 4,!             // returns 1
  WRITE "004.0" <= 4,!         // returns 1
  WRITE "++--4" <= 4,!         // returns 1
  WRITE "4 horsemen" <= 4,!    // returns 1

The following example shows equality comparisons of numeric strings. Non-canonical numeric strings are compared as character strings, not as numbers. Note that –0 is a non-canonical numeric string, and is therefore compared as a string, not a number:

  WRITE "4" = 4.00,!          // returns 1
  WRITE "004.0" = 4,!         // returns 0
  WRITE "++--4" = 4,!         // returns 0
  WRITE "4 horsemen" = 4,!    // returns 0
  WRITE "-4" = -4,!           // returns 1
  WRITE "0" = 0,!             // returns 1
  WRITE "-0" = 0,!            // returns 0
  WRITE "-0" = -0,!           // returns 0

Non-Numeric Strings

If the leading characters of the string are not numeric characters, the string’s numeric value is 0 for all arithmetic operations. For <, >, '>, <=, '<, and >= comparisons a non-numeric string is also treated as the number 0. Because the equal sign is used for both the numeric equality operator and the string comparison operator, string comparison takes precedence for = and '= operations. You can prepend the PlusSign property value (+ by default) to force numeric evaluation of a string; for example, "+123". This results in the following logical values, when x and y are different non-numeric strings (for example x=”Fred”, y=”Wilma”).

x, y x, x +x, y +x, +y +x, +x
x=y is FALSE x=x is TRUE +x=y is FALSE +x=+y is TRUE +x=+x is TRUE
x'=y is TRUE x'=x is FALSE +x'=y is TRUE +x'=+y is FALSE +x'=+x is FALSE
x<y is FALSE x<x is FALSE +x<y is FALSE +x<+y is FALSE +x<+x is FALSE
x<=y is TRUE x<=x is TRUE +x<=y is TRUE +x<=+y is TRUE +x<=+x is TRUE
FeedbackOpens in a new tab