Pat_RI
Pat_RI

Reputation: 11

XSLT - Look at last digit in number and replace with symbol

I have the XSLT below and need to update so that if the value is < 0 replace the last digit with a specific character. The replacing of the last character allows the vendor to tell the value is negative. For example if amount is 000000-111155 (-1111.55) I would need it to return 000000011115%. If it was a positive number I would need it to return 0000000111155.

My current XSLT is below:

<xsl:call-template name="prepend-pad">
<xsl:with-param name="length" select="13"/>
<xsl:with-param name="padType" select="'Zero'"/>
<xsl:with-param name="padVar">
<xsl:value-of
select="format-number(wd:Employee_CPP_QPP_Contribution*100,'#')"/>
</xsl:with-param>
</xsl:call-template>

I need to replace the format-number statement and not sure how to do so. Any help is greatly appreciated. The last character must be changed to indicate that the amount is negative.

0 = ' '
1 = '!'
2 = '"'
3 = '#'
4 = '$'
5 = '%'
6 = '&'
7 = "'"
8 = '('
9 = ')'

Upvotes: 1

Views: 922

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116959

Assuming a given integer $num, you could use something like:

<xsl:variable name="abs" select="translate($num, '-', '')" />
<xsl:value-of select="format-number($abs, '0000000000000')"/>
<xsl:if test="$num &lt; 0">
    <xsl:variable name="replace"> !"#$%&amp;'()</xsl:variable>
    <xsl:value-of select="translate($abs mod 10, '0123456789', $replace)"/>
</xsl:if>

to pad the absolute value of $num and to append an additional character when $num is negative.


Edit

To replace the last digit instead of appending, use:

<xsl:choose>
    <xsl:when test="$num &gt;= 0">
        <xsl:value-of select="format-number($num, '0000000000000')"/>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="format-number(floor(-$num div 10), '000000000000')"/>
        <xsl:variable name="replace"> !"#$%&amp;'()</xsl:variable>
        <xsl:value-of select="translate(-$num mod 10, '0123456789', $replace)"/>
    </xsl:otherwise>
</xsl:choose>

Upvotes: 2

Related Questions