XSLT format-number with comma

28,224

Solution 1

Use:

format-number(translate(., ',','.'), '#.###,000', 'd')

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:decimal-format name="d"
  decimal-separator="," grouping-separator="."/>

 <xsl:template match="/">
   <xsl:value-of select=
   "format-number(translate(., ',','.'), '#.###,000', 'd')"/>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<Add_Amount>2,59</Add_Amount>

produces the wanted result:

2,590

The problem with your code is that 2,59 isn't a valid number and must be converted to such, before passing this as the first argument of format-number().

Solution 2

The extra call to translate() in Dimitre Novatchev's answer seems unnecessary. The use of <xsl:decimal-format> should be enough, like this:

<xsl:decimal-format name="euroFormat" decimal-separator="," grouping-separator="."/>
<xsl:value-of select="format-number(text(), '###.###,00', 'euroFormat')"/>
Share:
28,224
Admin
Author by

Admin

Updated on August 16, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm trying to format this and other elements alike, so it would look like this 2,590:

    <Add_Amount>2,59</Add_Amount>
    

    Doing it like this:

    <xsl:decimal-format name="dkk" decimal-separator="," grouping-separator="."/>
    

    ....

        <xsl:value-of select="translate(format-number(Add_Amount, '#.###,000', 'dkk'), ',', '.')" />
    

    And the output comes out NaN. Any help is greatly appreciated.

    Thanks.

    //Daniel

  • JohannThor
    JohannThor about 9 years
    This is wrong: <xsl:decimal-format name="euroFormat" decimal-separator="," grouping-separator="."/> <xsl:value-of select="format-number('12345,5', '###.###,00', 'euroFormat')"/> returns NaN