String.ToDouble

From Xojo Documentation

Method

Returns the numeric equivalent of the source string. This function is the same as the Val function but is international-savvy.

Usage

result = stringVariable.ToDouble

Part Type Description
result Double The numeric equivalent of the string passed, if a numeric equivalent exists.
stringVariable String Any variable of type String.

Notes

For localized number formatting, use the ToDouble function instead. Generally, you will use Val for converting internal data and use ToDouble for converting data for input and output of user data.

See the Val function for more information.

ToDouble returns zero if string contains no numbers, except in the special case where the string begins with the string “NAN”. In this case, it returns "NAN(021)".

Numbers are converted only if they are found at the beginning of the string. Any numbers that follow a non-numeric value are ignored.

So,

 "1AA2" returns 1
 "AA2" returns 0
 "12AA54" returns 12

Sample Code

This code use the ToDouble function to return the numbers contained in a String.

Var source As String
Var result As Double

source = "12345"
result = source.ToDouble // returns 12345

source = "54.05car45"
result = source.ToDouble // returns 54.05

source = "123.45"
result = source.ToDouble // returns 123.45

source = "123 45"
result = source.ToDouble // returns 123

source = "123,456"
result = source.ToDouble // returns 123456

source = "auto"
result = source.ToDouble // returns 0

source = "&hFFF"
result = source.ToDouble // returns 4095

source = "&b1111"
result = source.ToDouble // returns 15

See Also

String.ToInt64, CStr, Double.FromString, Format, IsNumeric, Str, Val functions.