Search code examples
xmlxsltnumber-formatting

XSLT 1.0: number format issue


I have a question concerning the formatting of a number. In my source XML I have:

<ValueDeltAbsol>1.358.68842</ValueDeltAbsol>

Now this is actually 1.358.688,42. So my problem is that the decimal point is missing completely. So my idea was I remove the . with "translate()" first and then the last 2 digits are automatically the decimals.

I have tried the following

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

which gives me 135868842.00 and 135868842 and 135,868,842 respectively.

It just does not work to get the last 2 digits be the decimals. Can please someone tell me if it is possible to do what I try to achieve?

Input: <ValueDeltAbsol>1.358.68842</ValueDeltAbsol> expected output: <ValueDeltAbsol>1,358,688.42</ValueDeltAbsol>


Solution

  • If there is always going to be two digits that are the decimals you could divide the translated value by 100:

    <xsl:value-of select="format-number(number(translate(ValueDelt,'.','')) div 100,'###,###.00')" />
    

    Output:

    <ValueDeltAbsol>1,358,688.42</ValueDeltAbsol>