'Welcome to the Commodore Computer Wiki!'

VAL (value)

Keyword Token Version Category(s)
VAL $C5 1.0 Function

Syntax

VAL(string)

Where string is a character string literal, variable or expression.

Purpose

Convert a character string to floating-point number.

Return Value

Any valid floating-point value.

Notes

The string should contain characters representing a decimal number without any punctuation except for period (.), plus (+), minus (-), or ā€œeā€ (scientific format). To convert a string representing a hexidecimal number, see DEC().

Any spaces (character $20 = CHR$(32)) are ignored. Conversion stops at the first invalid character. If no valid numeric characters were found, the returned value is zero. This is important, as some characters will appear as a space on screen but if they precede the numeric characters, VAL() will return zero! Particularly bad are the non-breaking-space and cursor-right characters ($A0 = CHR$(160) and $1D = CHR$(29), respectively) – these usually appears exactly like a space on-screen! Other leading white space (tabs, line feed, carriage return) will cause the same problem, although they usually are obviously the problem when printed on-screen.

Examples

PRINT VAL("  12 3")
 123

PRINT VAL("  12,3")
 12

PRINT VAL("  12.3")
 12.3

PRINT VAL("1BED")
 1

PRINT VAL("BED")
 0

PRINT VAL("")
 0

Compare with DEC()
Contrast with STR$(), HEX$()


This page is a member of Commodore BASIC