Reputation: 45
I would like to format numbers so that always have 4 digits, but no decimal separators. Here is what I am trying to accomplish:
I have tried to use the format-number function, but with no success. Any tips on how to do it in XSLT 2.0/3.0?
Upvotes: 0
Views: 87
Reputation: 52878
Looks like you should multiply by 100 and format using the picture '0000'...
XML Input
<tests>
<test>0</test>
<test>2.5</test>
<test>10</test>
<test>6.75</test>
</tests>
XSLT
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="test">
<xsl:copy>
<xsl:value-of select="format-number(xs:double(.) * 100,'0000')"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XML Output
<tests>
<test>0000</test>
<test>0250</test>
<test>1000</test>
<test>0675</test>
</tests>
Fiddle: http://xsltfiddle.liberty-development.net/a9GPfQ/1
Not sure what you want to happen if you get a value larger than 99.99 though.
Upvotes: 2