Albert Jan Schot
 

XSLT and currency

07

Apr

Just some simple code this time, a minor XSLT function to change values to a nice Currency Value. Like it says, it formats a number (some var names are Dutch, sorry for that):

<xsl:template name="FormatNumber">

      <xsl:param name="str" />

      <xsl:param name="defnadekomma" />

 

      <xsl:variable name="newvalueS">

            <xsl:value-of select="string($str)" />

      </xsl:variable>

 

      <xsl:variable name="newvalue">

            <xsl:choose>

                  <xsl:when test="contains($newvalueS, ',')">

                        <xsl:value-of select="substring-before($newvalueS, ',')" />

                  </xsl:when>

                  <xsl:otherwise>

                        <xsl:value-of select="$newvalueS" />

                  </xsl:otherwise>

 

            </xsl:choose>

      </xsl:variable>

 

      <xsl:variable name="nadekomma" >

            <xsl:choose>

                  <xsl:when test="contains($newvalueS, ',')">

                        <xsl:value-of select="concat(',',substring-after($newvalueS, ','))" />

                  </xsl:when>

            </xsl:choose>

      </xsl:variable>

 

      <xsl:variable name="nadekommaMax" >

            <xsl:choose>

                  <xsl:when test="string-length($nadekomma) &gt; 3">

                        <xsl:value-of select="substring($nadekomma, 1, 3)" />

                  </xsl:when>

                  <xsl:otherwise>

                        <xsl:value-of select="$defnadekomma" />

                  </xsl:otherwise>

            </xsl:choose>

      </xsl:variable>

     

      <xsl:choose>

            <xsl:when test="string-length($newvalue) &gt; 3">

                  <xsl:variable name="newvaluews">

                        <xsl:value-of select="substring($newvalue,1,string-length($newvalue) - 3)" />.<xsl:value-of select="substring($newvalue,string-length($newvalue) - 2,3)" />

                  </xsl:variable>

                  <xsl:value-of select="concat($newvaluews, $nadekommaMax)" />

            </xsl:when>

            <xsl:otherwise>

 

                  <xsl:value-of select="concat($newvalue, $nadekommaMax)" />

 

            </xsl:otherwise>

      </xsl:choose>

</xsl:template>

 

By using the following statement you get a nice Euro sign, and a price, changing the € to a $ or whatever value you want it to be, you can even change the currency.

 

<xsl:text></xsl:text>

<xsl:call-template name="FormatNumber">

      <xsl:with-param name="str" select="$prijs" />

      <xsl:with-param name="defnadekomma" select="',-'" />

</xsl:call-template>

Albert-Jan Schot schreef

Comments (0)

Albert-Jan Schot