Product SiteDocumentation Site

8.3. Arithmetics

JSONiq supports the basic four operations, as well integer division and modulo. You should keep in mind that, as is the case in most programming languages, multiplicative operations have precedence over additive operations. Parentheses can override it, as explained above.
Example 8.4. Basic arithmetic operations with precedence override.
1 * ( 2 + 3 ) + 7 idiv 2 - (-8) mod 2
Results:
8

Dates, times and durations are also supported in a natural way.
Example 8.5. Using basic operations with dates.
date("2013-05-01") - date("2013-04-02")
Results:
"P29D"

If any of the operands is a sequence of more than one item, an error is raised.
If any of the operands is not a number, a date, a time or a duration, or if the operands are not compatible (say a number and a time), an error is raised.
Do not worry if the two operands do not have the same number type, JSONiq will do the adequate conversions.
Example 8.6. Basic arithmetic operations with different, but compatible number types
2.3e4 + 5
Results:
23005