Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT 1.0 amount fields have to have at least 2 decimals

Tags:

xslt-1.0

I have two xmls.There is a amount field which can contains values like 54.2,54.23,54.234,54.234567.

Would someone please tell me how can I make sure that atleast two decimal places will appear in the output xml.Currently 54.2 gets converted to 54,2 , but I want it to be 54,20

like image 678
C4CodeE4Exe Avatar asked Jan 20 '26 13:01

C4CodeE4Exe


1 Answers

You can use the format-number() function to convert a number into a string in a given format. At least two decimal places will be appeared if you use this "#.00##########" format string.

If you have an xml file which can contains values like 54.2,54.23,54.234,54.234567:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
        <price>54.2</price>
        <price>54.23</price>
        <price>54.234</price>
        <price>54.234567</price>
</catalog>

You can convert the numbers with an xslt like this to get at least two decimal places

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
      <xsl:for-each select="/catalog/price">
             <xsl:value-of select='format-number(., "#.00##########")'/><br />
      </xsl:for-each>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Then the output:

54.20
54.23
54.234
54.234567
like image 62
CsaByte Avatar answered Jan 23 '26 19:01

CsaByte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!