XSLT Reference
floor()
Returns the largest integer not greater than the argument — equivalent to rounding a number down toward negative infinity.
floor(number)Description
floor() returns the largest integer that is less than or equal to its argument. In plain terms, it rounds a number down toward negative infinity. For positive numbers this truncates the decimal part; for negative numbers this rounds away from zero.
The argument is first converted to a number using the same rules as number(). If the argument is already an integer, it is returned unchanged. If the argument is NaN or infinite, the same special value is returned.
Common uses include computing page numbers, calculating array indices from fractional results, and trimming calculated dimensions to integer pixel values.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
number | xs:double | Yes | The number to round down. |
Return value
xs:double — the largest integer value less than or equal to the argument.
Examples
Compute page count from item count
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<item>A</item>
<item>B</item>
<item>C</item>
<item>D</item>
<item>E</item>
</catalog>
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="pageSize" select="2"/>
<xsl:template match="/catalog">
<xsl:variable name="total" select="count(item)"/>
<pagination>
<total-items><xsl:value-of select="$total"/></total-items>
<full-pages><xsl:value-of select="floor($total div $pageSize)"/></full-pages>
</pagination>
</xsl:template>
</xsl:stylesheet>
Output:
<pagination>
<total-items>5</total-items>
<full-pages>2</full-pages>
</pagination>
Floor of positive and negative numbers
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<values>
<v>3.7</v>
<v>-3.7</v>
<v>5.0</v>
</values>
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/values">
<results>
<xsl:for-each select="v">
<floor of="{.}"><xsl:value-of select="floor(.)"/></floor>
</xsl:for-each>
</results>
</xsl:template>
</xsl:stylesheet>
Output:
<results>
<floor of="3.7">3</floor>
<floor of="-3.7">-4</floor>
<floor of="5.0">5</floor>
</results>
Notes
floor(-3.7)returns-4, not-3. Rounding is always toward negative infinity, not toward zero.- If the argument is
NaN,floor()returnsNaN. - If the argument is
Infinityor-Infinity, the same infinity is returned unchanged. floor()returns adoubletype in XPath 1.0, so the output may include a trailing.0on some processors when serialised. Useround()or integer arithmetic if you need a guaranteed integer format.- For rounding toward zero (truncation), there is no dedicated XPath 1.0 function; the common workaround is
floor($n)for positive numbers orceiling($n)for negative ones.