Method

# Val

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Returns the numeric form of a `string</api/data_types/string>`, always using US/English notation. For any user-visible numbers, you should use `String.ToDouble<string.todouble>` instead.

## Usage

``` xojo
result = Val(string)
```

**or**

``` xojo
result = stringVariable.Val
```

| Part           | Type                             | Description                                                                                                                                                                                                                                                                                                                |
|----------------|----------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| result         | `Double</api/data_types/double>` | The numeric equivalent of the string passed, if a numeric equivalent exists. If the passed `string</api/data_types/string>` does not contain a valid number, then the result is zero. The string is parsed by the operating system, which means that the behavior is platform-specific in the case of an undefined result. |
| string         | `String</api/data_types/string>` | Any valid `string</api/data_types/string>` expression.                                                                                                                                                                                                                                                                     |
| stringVariable | `String</api/data_types/string>` | Any variable of type `String</api/data_types/string>`.                                                                                                                                                                                                                                                                     |

## Notes

Val recognizes prefixes `&o</api/language/literals/o>` (octal), `&b</api/language/literals/b>` (binary), and `&h</api/language/literals/h>` (hexadecimal). However, spaces are not allowed in front of the ampersand. That is, " &hFF" returns 255, but "&h FF" returns 0.

For localized number formatting, use the `String.ToDouble<string.todouble>` function instead. Generally, you will use <span class="title-ref">Val</span> for converting internal data and use `String.ToDouble<string.todouble>` for converting data for input and output of user data.

It is important to note that <span class="title-ref">Val</span> does not take separator characters into consideration. For example:

``` xojo
Var d As Double
d = Val("10,000.95") ' The "," causes the string to stop being converted
```

returns 10. Use `String.ToDouble<string.todouble>` for data that is from the user as it may contain such characters. `String.ToDouble<string.todouble>` handles imbedded separators in the input string.

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

As <span class="title-ref">Val</span> converts the string to a double, large integer values in a string could exceed the maximum integer value that a double can hold. In these cases, use `String.ToInt64<string.toint64>` to convert longer integer strings to Int64 values.

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

Val also supports exponent notation:

``` xojo
Var n As Double

n = Val("1.0e-2") ' returns 0.01
n = Val("1.0e4")  ' returns 10000
```

## Sample code

These examples use the <span class="title-ref">Val</span> function to return the numbers contained in a `string</api/data_types/string>`.

``` xojo
Var n As Double
n = Val("12345") ' returns 12345
n = Val("54.25car45") ' returns 54.25
n = Val("123.25") ' returns 123.25
n = Val("123 25") ' returns 123
n = Val("123,456") ' returns 123
n = Val("auto") ' returns 0
n = Val("&hFFF") ' returns 4095
n = Val("&b1111") ' returns 15

Var s As String
s = "12345"
n = s.Val ' returns 12345
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`String.Val<string.val>`, `String.ToDouble<string.todouble>`, `String.ToInt64<string.toint64>`, `CStr</api/text/cstr>`, `IsNumeric</api/language/isnumeric>`, `Str</api/text/str>`, `Format</api/text/format>` functions; `&b</api/language/literals/b>`, `&h</api/language/literals/h>`, `&o</api/language/literals/o>` literals.

</div>
