Sur*_*rge 5 format xslt decimal
在执行 ceil 函数之前,我需要将小数点后 2 位(货币金额,因此最多只需要 2 位)存储在 XSL 变量中。
<xsl:element name="BaseFare"><xsl:value-of select="ceiling(BaseAmount/Amount * (1 - ($promoDisc div 100)))"/></xsl:element>
Run Code Online (Sandbox Code Playgroud)
例如。如果金额的结果是 499 并且 promoDisc = 8%,那么折扣金额将为 459.08 - 我需要将“08”(带零)存储在变量中以供稍后使用,同时返回上限金额(460)在输出 XML 中。我以为我可以只执行一个字符串函数并将小数点后的 2 个字符读入变量而不进行任何数学运算?
有不同的方法可以进行此提取:
I. 使用数字的字符串表示形式:
concat('.',substring(substring-after($x, '.'), 1, 2))
Run Code Online (Sandbox Code Playgroud)
二. 使用标准数学函数:
$x - floor($x)
Run Code Online (Sandbox Code Playgroud)
其计算结果为任何正数的小数部分$x。
使用以下函数之一:format-number()、round()、round-half-to-even()(最后一个函数仅适用于 XPath 2.0 / XSLT 2.0)将其四舍五入到小数点后两位。
在 XSLT 1.0 中,从正数精确获取小数点后两位小数的数字(截断而不四舍五入)的一种方法是:
format-number(
floor(100* $x) div 100
-
floor(floor(100* $x) div 100),
'.00'
)
Run Code Online (Sandbox Code Playgroud)
以下是所描述方法的完整示例:
<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="/">
<xsl:value-of select=
"concat('.',substring(substring-after(0.12543, '.'), 1, 2))"/>
=========
<xsl:value-of select=
"format-number(0.12543, '.00')"/>
=========
<xsl:value-of select=
"format-number(
floor(100* 999.12543) div 100
-
floor(floor(100* 999.12543) div 100),
'.00'
)
"/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当此转换应用于任何 XML 文档(未使用)时,结果为:
.12
=========
.13
=========
.12
Run Code Online (Sandbox Code Playgroud)