asp.net奇怪的计算结果

时间:2009-12-15 14:04:57

标签: asp.net math

在计算我们需要发票的小时数时,我在asp.NET中遇到了一个奇怪的问题。

在下面的示例中,我们总共需要75,9小时才需要开具发票。 这些小时分布在多个数据库行(TimeID)上。

基本上,我总是从每小时的“发票”中扣除“amount_invoiced”:

TimeID:25433 - to invoice=75,9 - amount_invoiced=1
TimeID:25774 - to invoice=74,9 - amount_invoiced=1
TimeID:24688 - to invoice=73,9 - amount_invoiced=1,5
TimeID:24646 - to invoice=72,4 - amount_invoiced=2
TimeID:24890 - to invoice=70,4 - amount_invoiced=2
TimeID:25773 - to invoice=68,4 - amount_invoiced=2,25
TimeID:24455 - to invoice=66,15 - amount_invoiced=2,5
TimeID:25431 - to invoice=63,65 - amount_invoiced=2,5
TimeID:24552 - to invoice=61,15 - amount_invoiced=3
TimeID:24644 - to invoice=58,15 - amount_invoiced=3
TimeID:24727 - to invoice=55,15 - amount_invoiced=3
TimeID:25000 - to invoice=52,15 - amount_invoiced=4
TimeID:25195 - to invoice=48,15 - amount_invoiced=4,15
TimeID:24510 - to invoice=44 - amount_invoiced=4,5
TimeID:24419 - to invoice=39,5 - amount_invoiced=5
TimeID:25126 - to invoice=34,5 - amount_invoiced=5,5
TimeID:25064 - to invoice=29 - amount_invoiced=6,5
TimeID:24420 - to invoice=22,5 - amount_invoiced=7
TimeID:25251 - to invoice=15,5 - amount_invoiced=7,5
TimeID:24897 - to invoice=8,00000000000001 - amount_invoiced=8

一切正常,TimeID 24897除外。 不知怎的计算15,5 - 7,5 = 8,00000000000001

我很困惑为什么会这样。有没有人遇到类似的问题?

2 个答案:

答案 0 :(得分:1)

听起来您使用了错误的数据类型来存储您的值......您应该使用decimal来获得准确的数学结果。

答案 1 :(得分:1)

通常的双重内部存储问题。在这个网站上已经多次回答了(我自己不止一次地回答过,而Skeet先生可能已经失去了数量)。例如:

Comparing double values in C#