本文介绍了XSLT 1.0 金额字段必须至少有 2 位小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 xml.有一个金额字段,可以包含 54.2,54.23,54.234,54.234567 之类的值.

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

有人请告诉我如何确保输出 xml 中至少出现两位小数.目前 54.2 被转换为 54,2 ,但我希望它是 54,20

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

推荐答案

您可以使用 format-number() 函数将数字转换为给定格式的字符串.如果使用这个#.00##########"格式字符串,至少会出现两位小数.

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.

如果您有一个 xml 文件,其中可以包含 54.2,54.23,54.234,54.234567 之类的值:

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>

您可以使用这样的 xslt 转换数字以获得至少两位小数

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>

然后输出:

54.20
54.23
54.234
54.234567

这篇关于XSLT 1.0 金额字段必须至少有 2 位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 13:56