tryParseNumber
tryParseNumber, function
def pure tryParseNumber(
source: text,
thousandSeparator: text,
decimalSeparator: text): (boolean, number)
def pure tryParseNumber(
source: text;
thousandSeparator: text,
decimalSeparator: text): (boolean, number)
Parses source as a number using the provided separators and returns a success
flag and the parsed value. When parsing fails, the number defaults to 0.
Examples
table Items = with
[| as Raw |]
[| "123" |]
[| "ABC" |]
[| "-1" |]
[| "2,56" |]
[| "2_000,5" |]
Items.Ok, Items.Num = tryParseNumber(Items.Raw; "_", ",")
show table "Parsed numbers" with
Items.Raw
Items.Ok
Items.Num
This outputs the following table:
| Raw | Ok | Num |
|---|---|---|
| 123 | true | 123 |
| ABC | false | 0 |
| -1 | true | -1 |
| 2,56 | true | 2.56 |
| 2_000,5 | true | 2000.5 |
This example handles mixed number conventions by reading the separators from each row.
table Items = with
[| as Raw, as Thousands, as Decimal |]
[| "1,234.5", ",", "." |]
[| "1.234,5", ".", "," |]
Items.Ok, Items.Num = tryParseNumber(Items.Raw, Items.Thousands, Items.Decimal)
show table "Parsed numbers" with
Items.Raw
Items.Ok
Items.Num
Both rows parse successfully to 1234.5.
Remarks
Use the success flag to detect parsing failures. The comma overload accepts row-specific separators. The semicolon overload takes scalar separators that apply to every row.