Skip to main content
XMLDir

Search 169 pages — formats, namespaces, parser messages, schema errors and tools.

XPath function

round(), floor() and ceiling()

The three rounding functions. round() goes to the nearest integer, breaking ties upward — including for negatives.

round() returns the nearest integer, with halves rounding toward positive infinity: round(-1.5) is -1, not -2. floor() and ceiling() round down and up respectively.

There is no way to round to a number of decimal places in XPath 1.0. The workaround is arithmetic: round(x * 100) div 100.

Syntax
round(number) → number
Returns
number
Kind
Function
Version
XPath 1.0 (libxml2)

Worked examples

Each result below is what the evaluator returned, asserted on every test run. An empty result means an empty node-set — which is an answer, not an error.

  • round(14.5)on order.xmlRun
    15
  • round(-1.5)on order.xmlRun
    -1

    Ties round toward positive infinity, so this is -1 rather than -2.

  • floor(14.9)on order.xmlRun
    14
  • ceiling(14.1)on order.xmlRun
    15
  • round(sum(//price) * 100) div 100on order.xmlRun
    53.5

    Rounding to two places, the only way available.

Run your own expression

What catches people out

  • round(-1.5) is -1. Ties go toward positive infinity, not away from zero.
  • There is no decimal-place argument and no format-number() — that is XSLT, not XPath.
  • round(NaN) is NaN, so a bad value survives rounding intact.

The sample documents

Every example on this page runs against this document. The sitemap one is namespaced on purpose — almost every real XML document is, and that changes which expressions match.

order.xml
<?xml version="1.0" encoding="UTF-8"?>
<order id="A-1001" tier="gold">
  <customer country="NO">Ada Lovelace</customer>
  <lines>
    <line sku="WIDGET-1" qty="2"><price>14.50</price></line>
    <line sku="WIDGET-2" qty="1"><price>39.00</price></line>
    <line sku="GIFT-WRAP" qty="1"><price>0.00</price></line>
  </lines>
  <note>  Deliver after 09:00  </note>
</order>

Related

All 34 XPath entries

Get started

Bring order to the XML your team can't afford to ignore.

Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.