【问题标题】:How to format-number without separators如何格式化没有分隔符的数字
【发布时间】:2020-05-04 22:32:25
【问题描述】:

我想格式化数字,以便始终有 4 位数字,但没有小数分隔符。这是我想要完成的任务:

  • 0 -> 0000
  • 2.5 -> 0250
  • 10 -> 1000
  • 6.75 -> 0675

我曾尝试使用 format-number 功能,但没有成功。有关如何在 XSLT 2.0/3.0 中执行此操作的任何提示?

【问题讨论】:

  • 向我们展示您的尝试,我们可以解释您的错误。

标签: xslt


【解决方案1】:

看来您应该乘以 100 并使用图片“0000”进行格式化...

XML 输入

<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 输出

<tests>
   <test>0000</test>
   <test>0250</test>
   <test>1000</test>
   <test>0675</test>
</tests>

小提琴:http://xsltfiddle.liberty-development.net/a9GPfQ/1

如果你得到一个大于 99.99 的值,不确定你想要发生什么。

【讨论】:

  • 使用 xs:double 可能更安全。这些值是非典型值,因为小数部分始终是四分之一的倍数,因此 xs:float 可能给出了精确的表示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
  • 2017-09-14
  • 2017-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多