XSL - 舍入/格式 - 数字问题 [英] XSL - rounding/format-number problem
问题描述
XML:< Quantity> 0.0050
我试图从一个数字中得到一个数字的值。 < / Quantity>
XSL:< xsl:value-of select =format-number数量,'#### 0.00')/>
然而,XSL似乎有这个值的问题,并输出 0.00
在页面的一个区域, 0.01
在另一个区域。当然,在这种情况下,在所有区域输出 0.01
是有利的。另外一个区域的值是 4.221
然而XSL输出 4.23
。
意识到 format-number
作为一种方法将一个数字转换为一个字符串。
不知道如何解决这个问题。
编辑:
确定后,我发现这个工作:
<$ p (round(100 * $ quantity)div 100,## 0.00)'/> $ p> < xsl:value-of select =
通过 website
正如这家伙所说的,XSL使用四舍五入的银行家大的。
这个解决方案看起来不够优雅,而且意味着将大量额外的功能添加到已经庞大而复杂的XSL文件中。当然我错过了什么?
不知道为什么格式会如此不一致,但从内存规格是.. 。复杂。同时,您可以使用回合
函数( org / xxl / XSLTreference / Output / function_round.htmlrel =noreferrer> ref )。这是不完美的,但功能。如果您需要特定数量的信号,您可以使用 MATHS POWER!并执行如下操作:
< xsl:value-of select =round(yournum * 100)div 100/>
I'm trying to get the value of a number to 2 dec places from my xml.
XML:<Quantity>0.0050</Quantity>
XSL:<xsl:value-of select="format-number($quantity, '####0.00')" />
However XSL seems to have a problem with this value and outputs 0.00
in one area of the page and 0.01
in the other. Of course in this situation it is favourable to have 0.01
output in all areas.
Another area has the value 4.221
yet the XSL is outputting 4.23
.
I do realise that format-number
as a method converts a number to a string.
Not sure how to fix this.
EDIT:
Ok after a bit of mucking around i found that this works:
<xsl:value-of select='format-number( round(100*$quantity) div 100 ,"##0.00" )' />
Via this website
As this guy mentions XSL uses 'bankers rounding' to round to even numbers instead of the bigger ones.
The solution hardly seems elegant, and means adding a ton of extra functions to an already bulky and complicated XSL file. Surely i'm missing something?
Not sure why format would be so inconsistent but from memory the spec for it is...complex.
Meantime, you can use the round
function (ref). Which is less than perfect, but is functional. If you need to have a particular number of sig figs you can use THE POWER OF MATHS! and do something like:
<xsl:value-of select="round(yournum*100) div 100"/>
这篇关于XSL - 舍入/格式 - 数字问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!