Search code examples
xpathnumbersnumber-formattingxpath-1.0

Round up in XPath 1.0


I'm dealing with a system where a number that is (even barely) over whole should be rounded up to the next-larger whole number. So

<Number>1.2</Number>
<Number>1.01</Number>
<Number>2</Number>
<Number>1.9183</Number>

should all be rounded to 2. How can I achieve this?


Solution

  • You can use the ceiling() function. It takes a number as an argument and it will replace the number by the next integer which is larger than the number.

    In XPath you also have the round() function which replaces the number by its nearest integer (larger or smaller), and floor() which is the opposite of ceiling().

    See: XPath Specification, 4.4 Number Functions