gpt4 book ai didi

c# - .net Math.Round 打印后的奇怪行为

转载 作者:行者123 更新时间:2023-11-30 15:29:59 25 4
gpt4 key购买 nike

这个问题既不是玩笑也不是垃圾邮件。在 HP OfficeJet Pro 8600 上打印后,我们注意到 Math.Round 的奇怪行为。这在 .net 2.0 和 .net 4.0 中观察到。

此代码可重现此问题。

double test = 1265.26777777;
MessageBox.Show("decimal : "+Math.Round(Convert.ToDecimal(test),2).ToString()+
" -- "+"double : "+Math.Round(test,2).ToString());

显示结果为1265.27 -- 1265.27 -> OK

我们打印一些副本到 HP 打印机(2 或 3 个打印作业)

现在的结果是 1265.27 -- 1265.26

这个 1 美分的差异可以用任何测试值重现。

有人开始对这种行为做出合理的解释吗?

在此先感谢您的帮助。

最好的问候,

家伙

最佳答案

解决方法如下:

[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]        
public static extern int _fpreset();

并在打印后调用 _fpreset() 解决了这个问题。

Hans Passant: That will reset the floating point processor.

这是正确的方法!参见 here有关 _fpreset 的信息。

感谢大家的成功集思广益。

关于c# - .net Math.Round 打印后的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22840866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com