Skip to main content

number


title: Number sidebar_position: 3

Number

Part of the function and operator catalog. Every example shown below is executed as an assertion by the engine's own test suite (examples.test.ts), so a result shown here is a result the engine actually produces for that input.

ABS

ABS(value: a number) -> a number

Returns a number's absolute value, keeping its original number of decimal places.

Examples:

  • ABS(-1.500)
    • result: "1.500"
  • ABS(5)
    • result: "5"

ROUND

ROUND(value: a number, places: a number, mode?: text) -> a number

Rounds a number to a declared number of decimal places. Defaults to commercial rounding (half-up, away from zero) unless an explicit rounding mode is given as the third argument. The places argument must not be negative.

Examples:

  • ROUND(2.345, 2)
    • result: "2.35"
  • ROUND(input.amount, 2)
    • context: {"amount":-2.345}
    • result: "-2.35"
  • ROUND(2.5, 0, 'half-up')
    • result: "3"
  • ROUND(2.5, 0, 'half-even')
    • result: "2"
  • ROUND(2.5, 0, 'half-down')
    • result: "2"
  • ROUND(2.5, 0, 'up')
    • result: "3"
  • ROUND(2.5, 0, 'down')
    • result: "2"
  • ROUND(2.5, 0, 'ceil')
    • result: "3"
  • ROUND(2.5, 0, 'floor')
    • result: "2"
  • ROUND(1, 0, 'bogus')
    • fails with EXPR_INVALID_ROUNDING_MODE
  • ROUND(1, -1)
    • fails with EXPR_TYPE_MISMATCH

PARSENUMBER

PARSENUMBER(text: text, format: text) -> a number

Reads a piece of text as a number, in one of three named formats: 'plain' (a dot decimal separator, no grouping), 'de' (a comma decimal separator, a dot grouping separator), or 'en' (a dot decimal separator, a comma grouping separator). This is the engine's only string-to-number conversion — a string is never guessed at. A single leading '+' or '-' sign and leading zeros are both accepted; no whitespace is accepted anywhere. The result's scale is the number of digits after the decimal separator in the source text.

Examples:

  • PARSENUMBER('1234.56', 'plain')
    • result: "1234.56"
  • PARSENUMBER('1234.56', 'en')
    • result: "1234.56"
  • PARSENUMBER('1234,56', 'de')
    • result: "1234.56"
  • PARSENUMBER('1,234.56', 'en')
    • result: "1234.56"
  • PARSENUMBER('1.234,56', 'de')
    • result: "1234.56"
  • PARSENUMBER('1.234,56', 'en')
    • fails with EXPR_NUMBER_PARSE_FAILED
  • PARSENUMBER('1,234.56', 'de')
    • fails with EXPR_NUMBER_PARSE_FAILED
  • PARSENUMBER('1,234.56', 'plain')
    • fails with EXPR_NUMBER_PARSE_FAILED
  • PARSENUMBER('1,23,456', 'en')
    • fails with EXPR_NUMBER_PARSE_FAILED
  • PARSENUMBER(',234.56', 'en')
    • fails with EXPR_NUMBER_PARSE_FAILED
  • PARSENUMBER('234,.56', 'en')
    • fails with EXPR_NUMBER_PARSE_FAILED
  • PARSENUMBER('1,,234', 'en')
    • fails with EXPR_NUMBER_PARSE_FAILED
  • PARSENUMBER('-1234.50', 'plain')
    • result: "-1234.50"
  • PARSENUMBER('+5', 'plain')
    • result: "5"
  • PARSENUMBER('-', 'plain')
    • fails with EXPR_NUMBER_PARSE_FAILED
  • PARSENUMBER('1-', 'plain')
    • fails with EXPR_NUMBER_PARSE_FAILED
  • PARSENUMBER('--5', 'plain')
    • fails with EXPR_NUMBER_PARSE_FAILED
  • PARSENUMBER('007.5', 'plain')
    • result: "7.5"
  • PARSENUMBER('1.', 'plain')
    • fails with EXPR_NUMBER_PARSE_FAILED
  • PARSENUMBER('.5', 'plain')
    • fails with EXPR_NUMBER_PARSE_FAILED
  • PARSENUMBER('1.2.3', 'plain')
    • fails with EXPR_NUMBER_PARSE_FAILED
  • PARSENUMBER(' 1', 'plain')
    • fails with EXPR_NUMBER_PARSE_FAILED
  • PARSENUMBER('1 ', 'plain')
    • fails with EXPR_NUMBER_PARSE_FAILED
  • PARSENUMBER('', 'plain')
    • fails with EXPR_NUMBER_PARSE_FAILED
  • PARSENUMBER('12a', 'plain')
    • fails with EXPR_NUMBER_PARSE_FAILED
  • PARSENUMBER('1.50', 'plain')
    • result: "1.50"
  • PARSENUMBER('1234.56', 'nonsense')
    • fails with EXPR_NUMBER_PARSE_FAILED