asNumber
Converts a numeric string to a number.
Studio attempts a string-to-number conversion whenever you input a string value to a function that requires a number, but there are times when you need to perform the conversion explicitly — for example, when comparing numbers that are stored as strings in your data.
The second parameter (optional) takes a locale. This allows you to convert numeric strings that don’t follow the English-language convention of using a period for the decimal separator and commas for thousand separators. Note that the locale is for parsing only; it does not apply formatting to the input number.
The function converts a currency string (e.g. '$100' or '100 USD') ONLY when a currency symbol or ISO currency code appears at the input string’s start or end — see Converting currency strings.
The input string can’t be an abbreviated number (e.g. '50K') — see asNumber and abbreviations.
Parameters
X (string)
The numeric string to convert to a number.
If the string includes a number enclosed in parentheses, the function returns the number part of the string as a negative number. For example, the input string '(500)' is parsed as -500.
LOCALE (string)
Optional. Determines which language the function uses to parse the input string. All supported locales are included in the parameter dropdown in the Function Builder.
Default: en_US
Examples
Assume the settings in Settings > Number and Currency are:
Setting | Value |
---|---|
LOCALE |
|
NUMBER FORMAT |
|
Important
The LOCALE setting in Settings > Number and Currency does NOT define the default value for the function's LOCALE parameter. The setting affects only the display format of the result.
ATL in Script | Result |
---|---|
| 500 |
| -5 |
| 1,234.57 |
| -1,234.57 |
None of the examples above specify a value for the second parameter, so the function parses using the default locale. See Using the LOCALE parameter for examples that use the second parameter.
Converting currency strings
The function discards valid currency codes or symbols that appear in the input string, then converts the remaining string to a number. It does this only when a valid code/symbol appears at the string's start or end.
ATL in Script | Result |
---|---|
| 500 |
| 500 |
| 1,000 |
| 1,000 |
| 1,234 |
| 1,234 |
| 1,234.56 |
| 1,234.56 |
None of the examples above specify a value for the second parameter, so the function parses using the default locale. See Using the LOCALE parameter for examples that use the second parameter.
Tip
The result is displayed as per the project settings. To re-apply currency formatting to the output number, use the currencyFormat function.
Negative numbers or currency values
The function returns a negative number if a minus sign precedes the number in your input string.
ATL in Script | Result |
---|---|
| -500 |
| -1,234 |
The function returns a negative number if parentheses surround the number in your input string.
ATL in Script | Result |
---|---|
| -500 |
| -1,234 |
asNumber and abbreviations
The input string should not be an abbreviated number.
ATL in Script | Result |
---|---|
| Error message: Cannot convert '250M' to a number. |
| Error message: Cannot convert '$3 billion' to a number |
Never use 'K' for thousand — 'K' is the Myanmar kyat's currency symbol.
ATL in Script | Result |
---|---|
| 250 |
Never use 'B' for billion — 'B' is the Panamanian balboa's currency symbol.
ATL in Script | Result |
---|---|
| 250 |
Tip
The function interprets '250K' to mean 250 kyats, and '250B' to mean 250 balboa. Never use 'K' or 'B' in your input string unless it represents a currency symbol.
Using the LOCALE parameter
You can use the LOCALE parameter to parse number/currency strings that don't follow the English-language convention of using a period for the decimal separator and commas for thousand separators.
For example, the convention in Germany is to use a comma for the decimal separator and periods for thousand separators. If your number/currency strings use this convention, you can parse them using the de_DE locale.
ATL in Script | Result |
---|---|
| 1,234.56 |
| 1,234.56 |
| 1,234,567.89 |
The function returns an error if the input string uses a different formatting convention to that used by the given locale.
ATL in Script | Result |
---|---|
| Error: Cannot convert '1.234,56' to a number. |
The given locale (en_GB) uses a period for the decimal separator and commas for thousand separators. The input string uses a different convention, so the function fails to parse the string.
Important
The LOCALE parameter is for parsing only. It doesn't affect the display format.