包含在双打列表中
我使用 Visual Studio 立即窗口来检查列表的内容。这是我可以看到的:
found_numbers
Count = 36
[0]: 50,82
[1]: 3358
[2]: 954
[3]: 5571
[4]: 3142
[5]: 700
[6]: 322
[7]: 402
[8]: 1231
[9]: 4118
[10]: 4532
[11]: 0
[12]: 0
[13]: 3101
[14]: 18
[15]: 0
[16]: 0
[17]: 8896
[18]: 0
[19]: 4,01
[20]: 19,5
[21]: 0,78
[22]: 20,28
[23]: 10
[24]: 27,76
[25]: 2,78
[26]: 30,54
[27]: 4648
[28]: 508
[29]: 1,51
[30]: 4648
[31]: 508
[32]: 1,51
[33]: 0,28
[34]: 0,28
[35]: 0,56
然后我检查两个变量的值
total1
50,82
total3
20,28
在最后一步中,我想查看我的列表是否包含这些值,这是我的输出:
found_numbers.Contains(total1)
false
found_numbers.Contains(total3)
true
我对这种行为感到非常困惑,因为这两个数字都进入了给定的列表。为什么会发生这种情况?编辑: total1 和 total3 都是 double 类型。
Im using visual studio Immediate Window to check the content of my List. This is what I can see:
found_numbers
Count = 36
[0]: 50,82
[1]: 3358
[2]: 954
[3]: 5571
[4]: 3142
[5]: 700
[6]: 322
[7]: 402
[8]: 1231
[9]: 4118
[10]: 4532
[11]: 0
[12]: 0
[13]: 3101
[14]: 18
[15]: 0
[16]: 0
[17]: 8896
[18]: 0
[19]: 4,01
[20]: 19,5
[21]: 0,78
[22]: 20,28
[23]: 10
[24]: 27,76
[25]: 2,78
[26]: 30,54
[27]: 4648
[28]: 508
[29]: 1,51
[30]: 4648
[31]: 508
[32]: 1,51
[33]: 0,28
[34]: 0,28
[35]: 0,56
Then I check the value of my two variables
total1
50,82
total3
20,28
In the last step, I want to see if my list contains those values and this is my output:
found_numbers.Contains(total1)
false
found_numbers.Contains(total3)
true
Im really confused about this behaviour as the both numbers are into the the given list. Why could this be happening? Edit: both total1 and total3 are type double.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我们使用
double
进行计算时,我们会得到舍入错误,这就是为什么我们实际上得到的是50,82000000001< 而不是
50,82
/code> 或50,81999999998
。所有这些值通常表示被四舍五入为50,82
,并且我们有奇怪的found_numbers.Contains(total1) == false< /代码>。要获得精确
double
值,请使用R
格式;尝试这些表示,您就会看到差异。如果您想摆脱舍入问题,您可以:
double
切换到decimal
(特别是如果值有固定小数点,例如如果值是货币)When we do computation with
double
we get rounding errors, and that's why instead of50,82
we actually have50,82000000001
or50,81999999998
. All these values are usually represented being rounded as50,82
and we have strangefound_numbers.Contains(total1) == false
. To get exactdouble
value useR
format;try these representations and you'll see the diffrence. If you want to get rid of rounding problems you can:
double
todecimal
(esp. if values have fixed decimal point, e.g. if values are currencies)