Val() function - MapBasic - 2023

MapInfo MapBasic Reference

Product type
Software
Portfolio
Locate
Product family
MapInfo
Product
MapInfo > MapBasic
Version
2023
Language
English
Product name
MapBasic
Title
MapInfo MapBasic Reference
First publish date
1985
Last updated
2023-09-12
Published on
2023-09-12T16:32:32.686312

Purpose

Returns the numeric value represented by a string. You can call this function from the MapBasic window in MapInfo Pro.

Syntax

Val( string_expr )

string_expr is a string expression.

Return Value

Float

Description

The Val() function returns a number based on the string_expr string expression. Val() ignores any white spaces (tabs, spaces, line feeds) at the start of the string_expr string, then tries to interpret the first character(s) as a numeric value. The Val() function then stops processing the string as soon as it finds a character that is not part of the number. If the first non-white-space character in the string is not a period, a digit, a minus sign, or an ampersand character (&), Val() returns zero. (The ampersand is used in hexadecimal notation; see example below.)

Note: If the string includes a decimal separator, it must be a period, regardless of whether the user's computer is set up to use some other character as the decimal separator. Also, the string cannot contain thousands separators. To remove thousands separators from a numeric string, call the DeformatNumber$() function.

Example

Dim f_num As Float
f_num = Val("12 thousand") 
' f_num is now equal to 12

f_num = Val("12,345") 
' f_num is now equal to 12 

f_num = Val(" 52 - 62 Brunswick Ave") 
' f_num is now equal to 52 

f_num = Val("Eighteen") 
' f_num is now equal to 0 (zero) 

f_num = Val("&H1A") 
' f_num is now equal to 26 (which equals hexadecimal 1A) 

See Also:

DeformatNumber$() function, Format$() function, Set Format statement, Str$() function