XSL - rounding/format-number problem

69,746

Solution 1

Not sure why format would be so inconsistent but from memory the spec for it is...complex.

Meantime, you can use the round function (ref). Which is less than perfect, but is functional. If you need to have a particular number of sig figs you can use THE POWER OF MATHS! and do something like:

<xsl:value-of select="round(yournum*100) div 100"/>

Solution 2

I've had endless trouble with decimals in XSLT/XPath 1.0, often a combination of it representing decimals as floating-point numbers and it using half-even rounding (banker's rounding). Unfortunately, the round(yournum*100) div 100 approach didn't work for me, due to floating-point imprecision. For example, multiplying 1.255 by 100 gives 125.49999999999999 (this isn't meant to be implementation dependent, as it's supposed to be IEEE 754, but I don't know if all implementations do adhere to that). When rounded, this then gives 125, rather than the desired 126.

I've taken the following approach, which I think works (although this is always a tricky area so I won't declare too much confidence!). However, it depends on your XSLT engine supporting EXSLT extensions. It presumes you want to round to two decimal places.

<func:function name="local:RoundHalfUp">
    <xsl:param name="number"/>

    <xsl:choose>
        <xsl:when test="contains($number, '.')">
            <xsl:variable name="decimal" select="estr:split($number, '.')[2]"/>
            <xsl:choose>
                <xsl:when test="string-length($decimal) &gt; 2">
                    <func:result select="format-number(concat($number, '1'), '0.00')"/>
                </xsl:when>
                <xsl:otherwise>
                    <func:result select="format-number($number, '0.00')"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
            <func:result select="format-number($number, '0.00')"/>
        </xsl:otherwise>
    </xsl:choose>

</func:function>

which can be called like:

<xsl:value-of select="local:RoundHalfUp(1.255)"/>

The namespaces are:

xmlns:func="http://exslt.org/functions"
xmlns:estr="http://exslt.org/strings"
xmlns:local="http://www.acme.org/local_function"

It's important to note that the function appends a '1', not adds 0.001 or similar.

Definately better to use XSLT 2.0 if it's an option (because it has a proper decimal type), but I know that's often not an option (from painful experience!).

Share:
69,746
Julio
Author by

Julio

Updated on August 02, 2020

Comments

  • Julio
    Julio over 3 years

    I'm trying to get the value of a number to 2 dec places from my xml.

    XML:<Quantity>0.0050</Quantity>

    XSL:<xsl:value-of select="format-number($quantity, '####0.00')" />

    However XSL seems to have a problem with this value and outputs 0.00 in one area of the page and 0.01 in the other. Of course in this situation it is favourable to have 0.01 output in all areas.

    Another area has the value 4.221 yet the XSL is outputting 4.23.

    I do realise that format-number as a method converts a number to a string.

    Not sure how to fix this.


    EDIT:

    Ok after a bit of mucking around i found that this works:

    <xsl:value-of select='format-number( round(100*$quantity) div 100 ,"##0.00" )' />
    

    Via this website

    As this guy mentions XSL uses 'bankers rounding' to round to even numbers instead of the bigger ones.

    The solution hardly seems elegant, and means adding a ton of extra functions to an already bulky and complicated XSL file. Surely i'm missing something?