Skip to main content

Strings as Numbers

Strings as Numbers

The following are the general rules for handling strings as numbers. For further details, see String-to-Number Conversion. For special processing, see Extremely Large Numeric Strings.

  • For all numeric operations, a string containing a number in canonical form is functionally identical to the corresponding number. For example, "3" = 3, "-2.5" = -2.5. (Note that -0 is not a canonical number.)

  • For arithmetic operations, a string containing only numeric characters in non-canonical form is functionally identical to the corresponding number. For example, "003" + 3 = 6, "++-2.5000" + -2.5 = -5.

  • For greater-than/less-than operations, a string containing only numeric characters in non-canonical form is functionally identical to the corresponding number. For example, the following statements are true: "003" > 2, "++-2.5000" >= -2.5.

  • For equality operations (=, '=), a string containing only numeric characters in non-canonical form is treated as a string, not a number. For example, the following statements are true: "003" = "003", "003" '= 3, "+003" '= "003".

Some further guidelines concerning parsing strings as numbers:

  • A mixed numeric string is a string that begins with numeric characters, followed by one or more non-numeric characters. For example “7 dwarves”. InterSystems IRIS numeric and boolean operations (other than equality operations) commonly parse a mixed numeric string as a number until they encounter a non-numeric character. At that point the rest of the string is ignored. The following example shows arithmetic operations on mixed numeric strings:

      WRITE "7dwarves" + 2,!   // returns 9
      WRITE "+24/7" + 2,!      // returns 26
      WRITE "7,000" + 2,!      // returns 9
      WRITE "7.0.99" + 2,!     // returns 9
      WRITE "7.5.99" + 2,!     // returns 9.5
  • A non-numeric string is any string in which a non-numeric character is encountered before encountering a numeric character. Note that a blank space is considered a non-numeric character. InterSystems IRIS numeric and boolean operations (other than equality operations) commonly parse this string as having a numeric value of 0 (zero). The following example shows arithmetic operations on non-numeric strings:

      WRITE "dwarves 7" + 2,!   // returns 2
      WRITE "+ 24/7" + 2,!      // returns 2
      WRITE "$7000" + 2,!       // returns 2
  • You can prefix a string with a plus sign to force its evaluation as a number for equality operations. A numeric string is parsed as a number in canonical form; a non-numeric string is parsed as 0. (A minus sign prefix also forces evaluation of a string as a number for equality operations; the minus sign, of course, inverts the sign for a non-zero value.) The following example shows the plus sign forcing numeric evaluation for equality operations:

      WRITE +"7" = 7,!            // returns 1 (TRUE)
      WRITE +"+007" = 7,!         // returns 1 (TRUE)
      WRITE +"7 dwarves" = 7,!    // returns 1 (TRUE)
      WRITE +"dwarves" = 0,!      // returns 1 (TRUE)
      WRITE +"" = 0,!             // returns 1 (TRUE)

Numeric string handling exceptions for individual commands and functions are common, as noted in the ObjectScript Reference.

Extremely Large Numeric Strings

Usually, a numeric string is converted to an ObjectScript Decimal value. However, with extremely large numbers (larger than 9223372036854775807E127) it is not always possible to convert a numeric string to a Decimal value. If converting a numeric string to its Decimal value would result in a <MAXNUMBER> error, InterSystems IRIS instead converts it to an IEEE Binary value. InterSystems IRIS performs the following operations in converting a numeric string to a number:

  1. Convert numeric string to Decimal floating point number. If this would result in <MAXNUMBER> go to Step 2. Otherwise, return Decimal value as a canonical number.

  2. Check the $SYSTEM.Process.TruncateOverflow()Opens in a new tab method boolean value. If 0 (the default) go to Step 3. Otherwise, return an overflow Decimal value (see method description).

  3. Convert numeric string to IEEE Binary floating point number. If this would result in <MAXNUMBER> go to Step 4. Otherwise, return IEEE Binary value as a canonical number.

  4. Check the $SYSTEM.Process.IEEEError()Opens in a new tab method boolean value. Depending on this value either return INF / -INF, or issue a <MAXNUMBER> error.

FeedbackOpens in a new tab