Xslt中遇到的怪现象

colee 2003-07-28 06:28:29
使用Xslt里的Sum函数,会有这样的结果:

33.447+33.4 +33.4 +33.4 =133.64700000000002
33.447+33.44 +33.4 +33.4 =133.687
33.447+33.447 +33.4 +33.4 =133.69400000000002
33.447+33.447 +33.447 +33.4 =133.741

即有时会自动精确到小数点后14位,有时又不会,本来只想正常相加,不用精确。
象33.447+33.447 +33.447 +33.4 =133.741 就满足我了;
究竟怎么回事呢?
示例请看:
http://www.channelace.com.hk/xml.xml
...全文
112 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
123456754321 2003-11-07
  • 打赏
  • 举报
回复
脚本语言不适合进行精确计算。如果需要,先转换成整数再进行计算

*1000,/1000
lions911 2003-09-14
  • 打赏
  • 举报
回复
学习!
colee 2003-09-04
  • 打赏
  • 举报
回复
即用VBscript没有问题,
<script language="VBscript">
msgBox(33.447+33.4 +33.4 +33.4)
</script>
colee 2003-08-19
  • 打赏
  • 举报
回复
另外ASP用的也是脚本语言,结果就很正常
response.write 33.447+33.4 +33.4 +33.4 结果是输出133.647
colee 2003-08-19
  • 打赏
  • 举报
回复
我部机一般modern , CPU 是Inter(R)Celeron(TM)1300MHZ

到底是 IE或Mozilla 的原因,XSLT或JS本身原因?还是硬件原因?
能有确切说法吗?
孟子E章 2003-08-18
  • 打赏
  • 举报
回复
Javascript, like almost all other programming languages, uses native
'floating-point' routines at some point to do mathematical calculations.
These native routines are generally perfomed either by the CPU or a
math-coprocessing chip.

There are always rounding errors associated with floating-point
calculations due to the fact that all numeric values have to be stored
in some fixed number of bits. So expressions such as
2.71894734524524523466451945 may be rounded to fewer decimal places
internally. In this case, the above multiplied by 1000 could result in
2178.954734525 instead of 2718.94734524524523466451945, depending on the
precision of the CPU.

Your equation, however, should be easily handled by any modern hardware
processor. Chances are you have either a browser with a very bad
Javascript engine, or a computer with a defective processor. A few years
ago, Intel released some PII processors that where found to have a
defect in performing floating point operations, producing the kind of
error you see. Check the above URL for help if you think this may be the
case.
孟子E章 2003-08-18
  • 打赏
  • 举报
回复
具体参见

http://support.intel.com/support/processors/pentium/fdiv/
孟子E章 2003-08-18
  • 打赏
  • 举报
回复
脚本语言不适合进行精确计算。如果需要,先转换成整数再进行计算
colee 2003-08-18
  • 打赏
  • 举报
回复

今天发现,原来在Javascript里也有类似情况:

<script>
alert(33.447+33.4 +33.4 +33.4); //结果弹出133.64700000000002
alert(33.447+33.44 +33.4 +33.4); //结果弹出133.687
</script>

难道一定要手动格式化???
colee 2003-08-01
  • 打赏
  • 举报
回复

具体有人知道怎么回事嘛?
有无不用自己格式化的方法?
colee 2003-07-29
  • 打赏
  • 举报
回复
多谢楼上的回复,自己格式化,多少都有点点误差和别扭
我试过,在 Mozilla 浏览器里也一样
gzh_seagull 2003-07-29
  • 打赏
  • 举报
回复
这是msxml的问题,你要自己格式化数据
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:decimal-format name="numfrmt" />
<xsl:template match="/">
<html>
<head>
<title>TEST</title>
</head>
<body leftmargin="0" topmargin="0">
33.447+33.4 +33.4 +33.4 =<xsl:value-of select="format-number(sum(Root/order/test), '.###', 'numfrmt')"/>
<br/>
33.447+33.44 +33.4 +33.4 =<xsl:value-of select="format-number(sum(Root/order1/test), '.###', 'numfrmt')"/> <br/>
33.447+33.447 +33.4 +33.4 =<xsl:value-of select="format-number(sum(Root/order2/test), '.###', 'numfrmt')"/> <br/>
33.447+33.447 +33.447 +33.4 =<xsl:value-of select="format-number(sum(Root/order3/test), '.###', 'numfrmt')"/>
<xsl:for-each select="order">
</xsl:for-each>
</body>
</html>

</xsl:template>
</xsl:stylesheet>
gzh_seagull 2003-07-29
  • 打赏
  • 举报
回复
估计是IE的bug,我试过用其他的转换器没有问题

8,909

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧