Skip to content
Paul Squires edited this page Oct 19, 2026 · 2 revisions

Home › Converting Data Types › Val

Val

Converts text to a double.

Syntax

Declare Function Val ( ByRef str As Const String ) As Double
Declare Function Val ( ByRef str As Const WString ) As Double

Parameters

  • strnum

Description

Val("10") will return 10.0, and Val("10.10") will return 10.1. The function parses the string from the left, skipping any white space, and returns the longest number it can read, stopping at the first non-suitable character it finds. Scientific notation is recognized, with "D" or "E" used to specify the exponent.

Val can be used to convert integer numbers in binary / octal / hexadecimal format, if they have the relevant identifier ("&B" / "&O" / "&H") prefixed, for example: Val("&HFF") returns 255.

Note: If you want to get an integer value from a string, consider using ValInt or ValLng instead. They are faster, since they don't use floating-point numbers, and only ValLng provides full 64-bit precision for LongInt types.

If you want to convert a number into string format, use the Str function.

Return Value

Returns a converted Double precision number

If the first character of the string is invalid, Val will return 0.

Remarks

Usage

result = Val( strnum )

Differences from QB

  • None

See also

Examples

Dim a As String, b As Double

a = "2.1E+30xa211"

b = Val(a)

Print a, b
2.1E+30xa211   2.1e+030

Category: Converting Data Types
Kind: function


Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.

Clone this wiki locally