双减双给出精度问题 [英] double minus double giving precision problems

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

问题描述

我在.NET中遇到了double的精度问题,我以为这仅适用于float,但现在我看到double是float.

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

处理此问题的正确方法是什么?圆形的?舍弃不需要的小数位?

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

推荐答案

使用

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."

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

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