C#浮动麻烦.为什么1000000 + 0.10f = 1000000? [英] C# float trouble. Why 1000000 + 0.10f = 1000000?
本文介绍了C#浮动麻烦.为什么1000000 + 0.10f = 1000000?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么
Console.WriteLine((1000000f + 0.10f).ToString("N2"));
打印
1 000 000.00
1 000 000.00
但没有1 000.000.10?
but no 1 000 000.10?
当我使用"double"类型或"float"类型时,减去1000000-此问题消失了!
When I use type "double" or type "float" less 1000000 - this problem disappears!
推荐答案
使用十进制可防止出现精度/舍入问题.
Use decimal to prevent those accuracy/rounding issues.
Console.WriteLine((1000000m + 0.10m).ToString("N2"));
原因:float仅具有7位数字的精度(引用)-您的电话号码有8
Reason: float has only a accuracy of 7 digits (reference) - your number has 8
这篇关于C#浮动麻烦.为什么1000000 + 0.10f = 1000000?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文