XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (559 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
11.75Mb size Format: txt, pdf, ePub

The function has been generalized to work with all numeric types.

Signature

Argument
Type
Meaning
value
Numeric?
The supplied number. If an empty sequence is supplied, an empty sequence is returned.
Result
Numeric?
The result of rounding down the supplied number to the integer below. The result has the same type as the supplied value
.

Effect

If the number is an
xs:integer
, or is equal to an
xs:integer
, then it is returned unchanged.

Otherwise, it is rounded down to the next lowest whole number. If the supplied value is an
xs:decimal
, the result will be an
xs:decimal
, if it is an
xs:double
, the result will be an
xs:double
, and if it is an
xs:float
, the result will be an
xs:float
. In the case of negative numbers, the rounding is away from zero.

The
xs:double
and
xs:float
types in XPath support special values such as infinity, negative zero and NaN (not-a-number), which are described on page 199 in Chapter 5.

If the argument is NaN (not-a-number), the result will be NaN. Similarly, when the argument is positive or negative infinity, the function will return the value of the argument unchanged.

Examples

Expression
Result
floor(1.0)
xs:decimal
1.0, displayed as
1
floor(1.6e0)
xs:double
1.0e0, displayed as
1
floor(17 div 3)
xs:decimal
5.0, displayed as
5
floor(-3.0)
xs:decimal
−3.0, displayed as
-3
floor(-8.2e0)
xs:double
−9.0e0, displayed as
-9
floor(number(‘NaN’))
xs:double
NaN

Usage

Like
round()
and
ceiling()
, this function is useful when calculating sizes of HTML tables.

Two alternatives you may want to consider are:

  • Using the
    xs:integer()
    constructor function. This differs from
    floor()
    in that it always truncates (rounds toward zero); also, it returns an actual integer, rather than returning a value of the same type as the argument.
  • Using the expression
    $x idiv 1
    . This produces the same result as the
    xs:integer()
    constructor function, but saves you from having to declare the XML Schema namespace.

Other books

Lust by Alyssa Rose Ivy
The Drinker by Fallada, Hans
Siren Song by Stephanie Draven
The Bridge by Jane Higgins
Summer Nights by Christin Lovell