Site Banner Graphic

Xslt Absolute Values

Getting the Absolute Value of a number using native Xslt functions

One way of doing it in Xslt 1.0. 

Here is an example of one way of getting the Absolute Value of some amount in Xslt.  This uses the Xslt function format-number(num) to change the numerical value to a string, ignoring any negating hyphens.  Once done, XPath function number(string) is called to return to a numerical value. 

<xsl:value-of select="number(format-number($Value, '###0.0###;#'))"/>

In the format string, '###0.0###;#', the semi-colon separates the format patterns for positive and negative numbers. The hash following the semicolon indicates a copy of the positive format, meaning both positive and negative numbers will be formatted to the same string. Use XPath to turn the string back into a number.

Users of Xslt 2.0 can just call the abs(num) function.