System Functions

NUM( )

Convert String to Value

Format

NUM(string$ [ , errval ] [,ERR=stmtref])

Where:

string$

Character string whose value is to be converted to a numeric value. Numeric in string expression (e.g. "19990317").

errval

Value to return if string$ is not numeric. Optionally can be an * (asterisk) to test string$. See Numeric String Testing.

stmtref

Program line number or statement label to which to transfer control.

Returns

Numeric value from string.

Description

The NUM( ) function returns the numeric value of a numeric expression in a string (e.g. 19990317 from "19990317"). The string is evaluated and converted to a numeric value.

If the value in string$ does not contain a valid numeric value, the system returns an Error #26: Variable type invalid. Your string expression can include any number of the following valid characters: 0 - 9, a space, a comma, an equals sign, or a dollar sign. You can also include one decimal point along with one sign character (either '-' or '+').

NUM( ) ignores the decimal point and thousands separator settings in the 'DP' and 'TH' system parameters and assumes the North American standard of "," for thousands and "." for the decimal point.

Scientific notation is supported; e.g. num("0.2E+10") returns a numerical value of 2000000000. As of PxPlus 2021, the + after the E is optional, and if omitted, the results will be the same as including the +; e.g. num("0.2E10") is the equivalent of num("0.2E+10"). Prior to PxPlus 2021, converting scientific notation only worked if the format included a + or - after the E.

(Support for optionally including + after the E was added in PxPlus 2021.)

Numeric String Testing

If an * (asterisk) is used in place of errval, the NUM( ) function does not convert the value of string$ but rather validates it can be converted to numeric. It will return a 0 if the value in string$ is not a valid numeric string, or 1 if it is a valid number.

(The ability to use errval or to specify an asterisk in the NUM function was added in PxPlus v6.30.)

Example

A=num("1.34")

Yields 1.34

A=num("-1,005.")

Yields -1005

A=num("A",err=0050)

On error, transfers to 0050 and sets err=26

A=num("A",1234)

Yields 1234

A=num("1.34",*)

Yields 1

A=num("A",*)

Yields 0