Str(), Val()
STR() and VAL() are reciprocal functions for converting numeric values to strings and vice versa. Both process null values correctly, returning nulls.
Usage |
cReturn = STR( nNumeric [, nLength [, nDecimals ] ] ) nReturn = VAL( cString ) |
Parameter |
Value |
Meaning |
nNumeric |
Numeric |
The numeric value to be converted. |
nLength |
Numeric |
The overall length of the resulting field, including preceding minus signs and any decimal places. Defaults to 10 if not specified. Limited to a maximum of 237. |
nDecimals |
Numeric |
Number of decimal places to be returned. If nDecimals is greater than (nLength minus 2), the length takes priority and no more than (nLength minus 2) decimal places are returned. Defaults to zero if not specified. Limited to a maximum of 18. |
cString |
Character |
A numeric value expressed in a string. Preceding plus or minus signs recognized. 16-character limit. |
? STR(1234567890123,7) && Returns ".1E+13" in VFP 3 ? STR(1234567890123,7) && Returns "1.E+12" in VFP 5 and later ? STR(1234567890123,12) && Returns ".123456E+13" in VFP 3 ? STR(1234567890123,12) && Returns "1.23456E+12" in VFP 5 and laterThis can be as useless as asterisks, because the exact value of the number has been lost for display purposes. If you're performing bulk conversions of numeric data to strings, make sure that you check for a return of asterisks or one containing the letter "E".
In versions prior to VFP 7, Help claims that the function truncates values. In actuality, STR() rounds fractional values ending in .50 up and .49 down. In all versions, STR(12345.6789,5) returns "12346", not 12345, and STR(12345.6789,7,2) returns "12345.7". The documentation finally matches the behavior in VFP 7. |
STR() first fits the whole number portion into the string, and what is left over is used for the decimal portion. You cannot assume that an expression STR(x,8,3) will always have the decimal point four spaces from the right end of the string—STR(1234.567,8,3) returns "1234.567", but STR(123456.7,8,3) returns "123456.7". We think this should result in a "Numeric Overflow" error, not in a string that looks different from the others. |
VAL() converts nearly all character strings to numbers—the only problem is that some of these numbers may be beyond the range FoxPro can actually work with. VAL("1E+307") creates a numeric without an error message, but this value cannot be stored in a numeric, double, float or integer field, nor can you do much useful processing on it. If you are converting character data to numeric in your applications, you need to range-check the result to ensure that you are working with legitimate numbers. |
This bug was introduced in VFP 5 and was fixed in VFP 7 Service Pack 1: Under some circumstances, passing VAL() a valid number in scientific notation or passing a character value would crash VFP. |
Example |
nHardDriveSize = VAL(SYS(2020)) cPageNum = "Page "+LTRIM(STR(_PAGENO)) lnWrongVal = VAL("123,456") && What's this value? lcPointSet = SET("POINT") && Preserve the setting SET POINT TO "." && Set to known value lnMyVal = VAL("12345.67") && Use it SET POINT TO lcPointSet && Restore the setting |
See Also |