web analytics

XSLT Template: Formatting currency amount in English and French Format

Options

codeling 1602 - 6666
@2019-06-13 11:41:05

The following XSLT template shows how to format to currency amount in and English and French format.

Note that the third argument in format-number() refers to the name of the <xsl:decimal-format> element:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" />
  <xsl:decimal-format name="french" decimal-separator="," grouping-separator="&#160;"/>

  <xsl:template name="FormatAmount">
    <xsl:param name="amount"/>
    <xsl:param name="language"/>
    <xsl:param name="show-dollar-sign">false</xsl:param>
    <xsl:variable name="dollar-sign">
      <xsl:if test="$show-dollar-sign = 'true' ">$</xsl:if>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$language = 'english' ">
        <xsl:value-of select="concat($dollar-sign, format-number($amount, '#,##0.00'))"/>
      </xsl:when>
      <xsl:when test="$language = 'french' ">
        <xsl:value-of select="concat(format-number($amount, '#&#160;##0,00', 'french'), ' ', $dollar-sign)"/>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
@2019-06-13 11:46:26

The <xsl:decimal-format> element defines the characters and symbols to be used when converting numbers into strings, with the format-number() function.

All countries do not use the same characters for separating the decimal part from the integer part, and for grouping digits. With the <xsl:decimal-format> element you can change special characters to other symbols.

This element is a top level element.

The format-number() function can refer to the <xsl:decimal-format> element by name.

<xsl:decimal-format
name="name"
decimal-separator="char"
grouping-separator="char"
infinity="string"
minus-sign="char"
NaN="string"
percent="char"
per-mille="char"
zero-digit="char"
digit="char"
pattern-separator="char"/>
Attribute Value Description
name name Optional. Specifies a name for this format
decimal-separator char Optional. Specifies the decimal point character. Default is "."
grouping-separator char Optional. Specifies the thousands separator character. Default is ","
infinity string Optional. Specifies the string used to represent infinity. Default is "Infinity"
minus-sign char Optional. Specifies the character to represent negative numbers. Default is "-"
NaN string Optional. Specifies the string used when the value is not a number". Default is "NaN"
percent char Optional. Specifies the percentage sign character. Default is "%"
per-mille char Optional. Specifies the per thousand sign character. Default is "‰"
zero-digit char Optional. Specifies the digit zero character. Default is "0"
digit char Optional. Specifies the character used to indicate a place where a digit is required. Default is #
pattern-separator char Optional. Specifies the character used to separate positive and negative subpatterns in a format pattern. Default is ";"
@2019-06-13 11:48:17

The format-number() function is used to convert a number into a string.

string format-number(number,format,[decimalformat])
Parameter Description
number Required. Specifies the number to be formatted
format Required. Specifies the format pattern. Here are some of the characters used in the formatting pattern:
  • 0 (Digit)
  • # (Digit, zero shows as absent)
  • . (The position of the decimal point Example: ###.##)
  • , (The group separator for thousands. Example: ###,###.##)
  • % (Displays the number as a percentage. Example: ##%)
  • ; (Pattern separator. The first pattern will be used for positive numbers and the second for negative numbers)
decimalformat Optional.
@2019-06-13 11:55:45
&#160; Non-breaking Space; &nbsp;

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com