XSL - 舍入/格式数字问题 [英] XSL - rounding/format-number problem

查看:26
本文介绍了XSL - 舍入/格式数字问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的 xml 中获取一个数字的值到 2 个十进制位置.

I'm trying to get the value of a number to 2 dec places from my xml.

XML:0.0050

XML:<Quantity>0.0050</Quantity>

XSL:<xsl:value-of select="format-number($quantity, '####0.00')"/>

然而,XSL 似乎对这个值有问题,在页面的一个区域输出 0.00,在另一个区域输出 0.01.当然在这种情况下最好在所有区域都有0.01输出.

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.

另一个区域的值为 4.221,但 XSL 输出的是 4.23.

Another area has the value 4.221 yet the XSL is outputting 4.23.

我确实意识到 format-number 作为一种将数字转换为字符串的方法.

I do realise that format-number as a method converts a number to a string.

不知道如何解决这个问题.

Not sure how to fix this.

好吧,经过一番折腾,我发现这行得通:

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" )' />

通过这个网站

正如这个人提到的 XSL 使用银行家舍入"来舍入到偶数而不是更大的数字.

As this guy mentions XSL uses 'bankers rounding' to round to even numbers instead of the bigger ones.

该解决方案似乎并不优雅,这意味着要向已经庞大而复杂的 XSL 文件添加大量额外的功能.我肯定错过了什么吗?

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.

同时,您可以使用 round 函数(参考).这并不完美,但功能齐全.如果您需要特定数量的无花果,您可以使用数学的力量!并执行以下操作:

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 - 舍入/格式数字问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆