[xsl] Re: Function that reduces the number of digits to the right of the decimal point?

Subject: [xsl] Re: Function that reduces the number of digits to the right of the decimal point?
From: "Roger L Costello costello@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 23 Jun 2020 16:53:24 -0000
Hi Folks,

Thank you for your excellent feedback!

Thank you Michael Kay for pointing out the round versus truncate issue. I
asked my customer which they want (round or truncate) and they want the option
of doing either. So I added a third argument to my function $round-or-truncate
and now the function can do either. Here is the updated function which
incorporates your suggestions:
--------------------------------------
<xsl:function name="f:reduce-number-of-digits-after-decimal-point">
    <xsl:param name="num" />
    <xsl:param name="desired-number-of-digits-after-decimal-point"
as="xs:integer" />
    <xsl:param name="round-or-truncate" as="xs:string" />

    <xsl:choose>
        <xsl:when test="$round-or-truncate eq 'round'">
            <xsl:value-of select="round($num,
$desired-number-of-digits-after-decimal-point)"/>
        </xsl:when>
        <xsl:when test="$round-or-truncate eq 'truncate'">
            <xsl:variable name="multiplication-factor"
select="xs:integer(math:pow(10,
$desired-number-of-digits-after-decimal-point))" as="xs:integer"/>
            <xsl:value-of select="($num*$multiplication-factor idiv 1) div
$multiplication-factor" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="error(xs:QName('round-or-truncate'),
'Invalid value for $round-or-truncate')"/>
        </xsl:otherwise>
    </xsl:choose>

</xsl:function>

Current Thread