Wiki source for KeyPgVal


Show raw source

{{fbdoc item="title" value="VAL"}}----
Converts a string to a floating point number

{{fbdoc item="syntax"}}##
[[KeyPgDeclare|declare]] [[KeyPgFunction|function]] **Val** ( [[KeyPgByref|byref]] //str// [[KeyPgAs|as]] [[KeyPgConstQualifier|const]] [[KeyPgString|string]] ) [[KeyPgAs|as]] [[KeyPgDouble|double]]
[[KeyPgDeclare|declare]] [[KeyPgFunction|function]] **Val** ( [[KeyPgByref|byref]] //str// [[KeyPgAs|as]] [[KeyPgConstQualifier|const]] [[KeyPgWstring|wstring]] ) [[KeyPgAs|as]] [[KeyPgDouble|double]]
##
{{fbdoc item="usage"}}##
//result// = **Val**( //strnum// )
##
{{fbdoc item="param"}}
##//strnum//##
the string containing a number to convert

{{fbdoc item="ret"}}
Returns a converted ##[[KeyPgDouble|Double]]## precision number

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

{{fbdoc item="desc"}}
##**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 ##[[KeyPgValint|ValInt]]## or ##[[KeyPgVallng|ValLng]]## instead. They are faster, since they don't use floating-point numbers, and only ##[[KeyPgVallng|ValLng]]## provides full 64-bit precision for ##[[KeyPgLongint|LongInt]]## types.

If you want to convert a number into string format, use the ##[[KeyPgStr|Str]]## function.

{{fbdoc item="ex"}}
{{fbdoc item="filename" value="examples/manual/strings/val.bas"}}%%(freebasic)
dim a as string, b as double
a = "2.1E+30xa211"
b = val(a)
print a, b
%%

%%
2.1E+30xa211 2.1e+030
%%

{{fbdoc item="diff"}}
- None

{{fbdoc item="see"}}
- ##[[KeyPgCdbl|Cdbl]]##
- ##[[KeyPgValint|Valint]]##
- ##[[KeyPgValuint|Valuint]]##
- ##[[KeyPgVallng|Vallng]]##
- ##[[KeyPgValulng|Valulng]]##
- ##[[KeyPgStr|Str]]##
- ##[[KeyPgChr|Chr]]##
- ##[[KeyPgAsc|Asc]]##

{{fbdoc item="back" value="CatPgCasting|Converting Data Types"}}{{fbdoc item="back" value="CatPgString|String Functions"}}
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode