ReadyBASIC
BASIC Language Reference Manual > Command and Function Reference >

VAL Function

Summary

Converts a string containing a representation of a numeric value into a numeric value.

Syntax

VAL(string expression)

Remarks and Examples

Given a string that contains a represntation of an integer or floating-point number, the VAL function converts it into a numeric value. The returned numeric value could be stored in an integer or floating-point variable, or used in a mathematical expression.

10 A$ = "33.45"
20 A = VAL(A$)
30 PRINT A
RUN
33.45

Trailing and leading spaces are ignored by VAL:

PRINT VAL("    25    ")
25

If VAL is given a string that does not contain a valid representation of a number, VAL will return 0. This includes the case where multiple numeric values may be present in the string:

PRINT VAL("Junk")
0.0
PRINT VAL("25 45.6 300")
0.0

VAL only recognizes period characters (".") as decimal points; "European"-style decimals using commas are not recognized.

PRINT VAL("10,75")
0.0

To convert a numeric value into a string, use the STR$ function.

Note: Although the current version of ReadyBASIC does not support scientific notation in expressions in program listings, VAL will correctly handle strings containing values expressed in scientific notation:

PRINT VAL("3.5E3")
3500.0
PRINT VAL("3.5E-3")
0.0035

Full support for scientific notation will be added in a later version of ReadyBASIC.




Copyright 2006-2008, Kevin Matz, All Rights Reserved.