Convert string to number

Syntax

int|long|double val(s [type] [length])

Parameters

s - string that contains a number at the beginning.

type - return type: 0 - int, 1 - long, 2 - double. Default: 0. Constant.

length - int variable that receives the number of characters from the beginning of s to the end of the part of s that contains the number. Receives 0 if s does not begin with a number or the number is too big.

Remarks

Returns numeric value of string s. Returns 0 if the string does not begin with a number or the number is too big.

The string can contain integer or double (floating-point) number. Integer number can be decimal (like 10) or hexadecimal (like 0x1C). Double number can be in form

[sign][digits][.digits][(d|D|e|E)[sign]digits]

Whole string can begin with spaces or tabs and sign - or +.

Regardless of type, the function always parses whole number in all supported formats. For example, val("1.5E3") returns 1500. It uses type only to know function's return type and check bounds.

QM 2.3.0. If number is 0x80000000-0xFFFFFFFF when type is 0, or 0x8000000000000000-0xFFFFFFFFFFFFFFFF when type is 1, the function returns a negative value. In older QM versions would return 0 (number too big).

Tips

To convert number to string, use operator = with a str variable. Example: int i=5; str s; s=i ;;now s is "5".

Example

str s1 s2; int i length; double d
s1="-100"
i=val(s1); out i
s2="4.757E2"
d=val(s2 2 length); out d; out length
Output:
-100
475.7
7