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.

Example

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

Remarks

Use the success flag instead of relying on the default number. The semicolon overload is convenient when the separators are constant.

See also

User Contributed Notes
0 notes + add a note