XSLT and XPath function reference in alphabetical order
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 5, translated from German)
Rounding functions for numeric types
A number of numeric type; the next higher or lower nearest integer of an input number after rounding, but without change of the data type. In XPath 1.0 the return value is of number type.
Optional. The number to be rounded. In XPath 2.0 this number must correspond to one of the numeric data types or a type derived thereof. Where applicable, a type promotion is tried.
Purpose of use:
The fn:round() function rounds a decimal number passed on to it up or down to the next integer. The data type of the number is maintained. If the input number is already an integer, the value remains unchanged. If the input type is a type derived from one of the numeric base types, the rounding result is returned typed with the appropriate base type.
If the input value has the xs:untypedAtomic type, this type is cast to xs:double by means of type promotion.
If the type of the input value is not xs:untypedAtomic or one of the four numeric types xs:float, xs:double, xs:decimal or xs:integer or a typed derived from these types, a type error is reported.
If the empty sequence is passed on to the function, it also returns the empty sequence. If (for the input types xs:double or xs:float) the input value is NaN, NaN is also returned.
When rounding to 0, the rounding direction has an influence on the result. So, a value rounded up from a number greater than -0,5 against 0 results in -0 (negative zero), in contrast a value rounded down from a number less than +0,5 against 0 results in +0 (positive zero). Positive and negative zero values of xs:double or xs:float type remain unchanged as input values as well as positive infinite INF or negative infinite -INF.
Behaviour under XPath 1.0:
In XPath 1.0 for round() an implicit type conversion according to the number() function for non-numeric input values is made. If in XPath 1.0 the value passed on is an empty string, the result is NaN. (Attention: the similar passing on of an empty sequence under XPath 2.0 results in an empty sequence!)
|fn:round(-0.499999)||-0 (negative zero)|
|fn:round(0.499999)||0 (positive zero)|
|fn:round('string')||Type error in XPath 2.0|
|round('string') (XPath 1.0)||NaN in XPath 1.0|
Table: examples for the call of fn:round()
fn:round($arg as numeric?) as numeric?
|<< back||next >>|
Copyright © Galileo Press, Bonn 2008
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "XSLT 2.0 & XPath 2.0" is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn, Germany