toNumber Method

Class: String

Description

This method converts a string to a number.

 

If the string contains a number created with Number.toString(), then this method will inverse the operation and return the original number. However, if the string contains an arbitrary sequence of characters, then the number returned is derived by applying the following algorithm to the string:

Element

Description or Associated Action

signs

Any sequence of plus sign and minus signs. If the total number of minus signs is odd, the result of the following elements will be negated.

zero or more digits

The integer part of the number.

decimal dot

A single decimal dot is expected.

optional fractional digits

Zero or more can appear.

uppercase E

An exponent.

Optional single sign

The sign of the exponent.

One or more digits of the exponent

 

Note that these operations may transform a negative numeric interpretation into a positive value.

Syntax

StringObject.toNumber(String mask,Boolean stripSpace)

Parameters

mask

An editing mask that describes how the number is formatted within the string. This uses mask syntax from $$NUM^%ZM.

 

Position

Interpretation

 

1

Decimal representation. Translates to a decimal dot. If you supply a mask, always supply the correct value used within the string, even if the default (dot) is used.

 

2

Thousands separator. This character will be unconditionally removed when translating the string into a number. Use the , symbol for most applications.

 

3

Formatting used for negative numbers. This character can have the following values:

  • T - Expect a trailing rather than a leading sign. If the string ends with a minus, then the numeric interpretation will be negated.

  • P - Expect negative values in parentheses. If the string ends with a closing parenthesis, then the numeric interpretation of the string will be negated. This character causes the removal of all spaces and parentheses when translating the string into a number.

 

4

Character used to represent currencies. This character will be unconditionally removed when translating the string into a number.

stripSpace

An option that indicates whether spaces are removed when the string is converted to a number. Valid values are 1 (remove spaces) and 0 (do not remove spaces). The default is 0.

Returns

If the string is empty, then it returns the empty string. Otherwise, it returns the canonic representation of the numeric interpretation of the string, after applying mask and stripSpace.

When Became Available

v7.0

Example

type String STR = "(12.345,67)"        // negative, European

type Number NUM = STR.toNumber(",.P")  // -12345.67

 

set STR = "   (12.345,67)"             // add. space ignored by "P"

set NUM = STR.toNumber(",.P")          // -12345.67

 

set STR = "    12.235,67 "             // same number, but positive

set NUM = STR.toNumber(",.P")          // 12345.67

 

set STR = "$12,345.67-"                // negative trailing, US format

set NUM = STR.toNumber(".,T$")         // -12345.67

 

set STR = "    $12,345.67-"            // add. space NOT ignored by "T"

set NUM = STR.toNumber(".,T$")         // 0

set NUM = STR.toNumber(".,T$",1)       // stripSpace: -12345.67

 

set STR = "-123.45E6"                  // exponent

set NUM = STR.toNUmber()               // -123450000

 

set STR = "-123.45e6"                  // lowercase e!

set NUM = STR.toNUmber()               // -123.45

 

set STR = "-0123.4560"                 // insignificant zeros

set NUM = STR.toNUmber()               // -123.456

 

set STR = "123.456ABC"                 // trailing alphas

set NUM = STR.toNUmber()               // 123.456

 

set STR = "-123.456ABC-"               // "T" AND minus sign

set NUM = STR.toNumber(",.T")          // 124.456