xslt - How to change all decimal to Zeroes using XSL -
how change decimal value zeroes in xsl
example value:
from : 9876.123
to : 9876.000
in case number of digits after decimal point unknown in advance, use:
concat(substring-before(., '.'), '.', translate(substring-after(., '.'), '123456789', '000000000'))
here complete xslt transformation example:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="d"> <xsl:value-of select= "concat(substring-before(., '.'), '.', translate(substring-after(.,'.'), '123456789','000000000'))"/> </xsl:template> </xsl:stylesheet>
when transformation applied on following xml document:
<t> <d>9876.1</d> <d>9876.12</d> <d>9876.123</d> <d>9876.1234</d> <d>9876.12345</d> <d>9876.123456</d> <d>9876.1234567</d> <d>9876.12345678</d> <d>9876.123456789</d> </t>
the wanted, correct result produced:
9876.0 9876.00 9876.000 9876.0000 9876.00000 9876.000000 9876.0000000 9876.00000000 9876.000000000
update
someone requested integer values (not containing decimal point) processed correctly (copied intact).
i added negative values , / or currency denominations should processed correctly.
although falls outside scope of current problem, here again single xpath 1.0 expression produces wanted result:
concat(substring-before(concat(., '.'), '.'), translate(., '+-$0123456789', ''), translate(substring-after(.,'.'), '123456789','000000000'))
and here again complete transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="d"> <xsl:value-of select= "concat(substring-before(concat(., '.'), '.'), translate(., '+-$0123456789', ''), translate(substring-after(.,'.'), '123456789','000000000'))"/> </xsl:template> </xsl:stylesheet>
when transformation applied on following xml document:
<t> <d>-$1.234</d> <d>-1.234</d> <d>-.234</d> <d>9876</d> <d>9876.1</d> <d>9876.12</d> <d>9876.123</d> <d>9876.1234</d> <d>9876.12345</d> <d>9876.123456</d> <d>9876.1234567</d> <d>9876.12345678</d> <d>9876.123456789</d> </t>
the wanted, correct result produced:
-$1.000 -1.000 -.000 9876 9876.0 9876.00 9876.000 9876.0000 9876.00000 9876.000000 9876.0000000 9876.00000000 9876.000000000