Numeric and Math Functions

The numeric family covers abs(), ceiling(), floor(), round() and round-half-to-even() (banker's rounding), the aggregates sum(), avg(), min() and max(), and formatting with format-number() (decimal pictures) and format-integer() (words, Roman numerals, ordinals). math:pi(), math:sqrt(), math:pow(), math:exp(), math:log(), math:log10() and the trigonometric functions sit in the math: namespace and work in xs:double. On untyped prices, the type of the input decides the type of the answer:

Aggregates in double and decimal arithmetic, rounding and formatting
xp 'sum(//price)' 'sum(//price ! xs:decimal(.))' 'avg(//price ! xs:decimal(.))' \
   'round(2.5), round(-2.5)' 'round-half-to-even(2.5)' 'round(8452, -2)' \
   'format-number(sum(//price ! xs:decimal(.)), "$#,##0.00")' \
   'format-integer(57, "I")' 'math:sqrt(2)'
Output
sum(//price)                           => 1.3474e2
sum(//price ! xs:decimal(.))           => 134.74
avg(//price ! xs:decimal(.))           => 22.45666666666666666667
round(2.5), round(-2.5)                => 3 -2
round-half-to-even(2.5)                => 2
round(8452, -2)                        => 8500
format-number(sum(//price ! xs:decimal(.)), "$#,##0.00") => "$134.74"
format-integer(57, "I")                => "LVII"
math:sqrt(2)                           => 1.4142135623730951e0

Untyped values are summed as doubles; cast to xs:decimal first and the arithmetic is exact, the rule for money in every pipeline. round() rounds halves toward positive infinity (so -2.5 becomes -2), while round-half-to-even() avoids the upward bias that matters when you round thousands of line items. A negative precision rounds to tens or hundreds. Older reference tables get some of these wrong (a negative -8500 here, or format-integer() for decimal pictures), so test on Saxon 726,956 before you trust a table.


Numeric Functions

Numeric functions perform rounding, basic conversion, and number-to-string formatting. See also Math Functions for exponential, logarithmic, and trigonometric operations.
abs(-3)3
ceiling(1.1)2
floor(1.9)1
round(1.5)2
round(1.125,2)1.13
round(8452,-2)-8500
round-half-to-even(2.5)2
round-half-to-even(35612.25, -2)35600
number("15")15
format-integer(123,'0000')0123
format-integer(123,'w')one hundred and twenty-three
format-integer(21,'1;o','en')21st
format-integer(14,'Ww;o(-e)','de')Vierzehnte
format-integer(7,'a')g
format-integer(57,'I')LVII
format-integer(1234,'#;##O;')1;234
format-number(12345.6, '#,###.00')12,345.60
format-integer(12345678.9, '9,999.99')12,345,678.90
format-integer(123.9,'9999')0124
format-integer(0.14,'01%')14%
format-integer(-6,'000')-006
format-integer(12.34,'0.000e00')1.234e01
format-integer(12.34,'#.000e0')0.123e2
format-integer(1234.5678, '#;##0*00', 'ch')1;234*57

Values courtesy of http://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/#func-number 189 .

ch03-numeric-functions.xpath.txt
merged-01-01-ch03-numeric-functions.xpath.txt
round(1.125, 2)
Output
1.13

Math Functions

Math functions provide the constant pi and common exponential, logarithmic, power, root, and trigonometric operations. They live in the http://www.w3.org/2005/xpath-functions/math 189 namespace, conventionally bound to the math: prefix. See also Numeric Functions for rounding and formatting.
2*math:pi()6.283185307179586e0
math:exp(1)2.7182818284590455e0
math:exp10(1)1.0e1
math:log(math:exp(1))1.0e0
math:log10(1.0e3)3.0e0
math:pow(2,3)8.0e0
math:sqrt(1.0e6)1.0e3
math:sin(math:pi())0.0e0
math:cos(math:pi())-1.0e0
math:tan(math:pi())0.0e0
math:asin(1.0e0)1.5707963267948966e0
math:acos(1.0e0)0.0e0
math:atan(1.0e0)0.7853981633974483e0
math:atan2(-0.0e0,+1)-0.0e0
ch03-math-functions.xpath.txt
merged-02-01-ch03-math-functions.xpath.txt
math:sqrt(1.0e6)
Output
1.0e3