C#(4):双减双精度问题 [英] C# (4): double minus double giving precision problems

查看:124
本文介绍了C#(4):双减双精度问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.NET中遇到了一个精简问题,我以为这只适用于浮点数,但现在我看到double是一个浮点数。

I have come across a precision issue with double in .NET I thought this only applied to floats but now I see that double is a float.

double test = 278.97 - 90.46;
Debug.WriteLine(test) //188.51000000000005

//correct answer is 188.51

正确的处理方法是什么?回合? Lop off不必要的小数位?

What is the correct way to handle this? Round? Lop off the unneeded decimal places?

推荐答案

使用 decimal 数据类型。 十进制值类型适用于需要大量重要积分和小数位数的财务计算,而且无差错误差。

Use the decimal data type. "The Decimal value type is appropriate for financial calculations requiring large numbers of significant integral and fractional digits and no round-off errors."

这篇关于C#(4):双减双精度问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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