XSL 转换格式数字,去除小数并添加前导零

4

我有一个问题,需要将数字1234.56格式化为123456。这件事我做得到,同时我还需要在数字前面填充零以占用12个空格。我也能做到。问题是如何将这两个过程合并以格式化数字和添加填充?

我的代码:

<xsl:value-of select="format-number(substring(concat(Payment, $Pad50), 1, 12), "0")"/> 

我遇到了一个错误:

元素类型 "xsl:value-of" 后必须跟随属性规范,">" 或 "/>"。


你的意思是要移除小数点吗? - user207421
是的,删除并填充零。 - user5195397
1个回答

4

这应该可以工作

<xsl:value-of select="format-number(Payment * 100, '000000000000')"/> 

这里的思路是将 Payment 节点的小数点向右移动两位(乘以 100),然后告诉 format-number 填充数字,使其达到 12 个字符长度(前面带有 0)。唯一潜在的问题是,如果 Payment 有时非常大,乘以 100 可能会导致溢出——但您的 12 位数限制表明这不应该成为问题。
您原来的代码有一些问题:
  1. You were passing the last argument to format-number incorrectly - use a ' instead of ", otherwise the processor thinks you're closing the select attribute and doesn't know what to do with the 0 and another ".
  2. concat and substring are going to return strings - format-number expects a number as the first argument. If you cast the string to number, you'll lose all your padded zeroes.
  3. You could use a different set of functions - assuming $Pad50 is a string of twelve zeroes:

    <xsl:value-of select="substring(translate(concat($Pad50, Payment), '.', ''), string-length(Payment), 12)" />
    
但这会变得很混乱。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接