【发布时间】:2012-12-02 04:58:17
【问题描述】:
可能重复:
What is the difference between Decimal, Float and Double in C#?
帮帮我。
我正在用 C# 开发一个应用程序。我正在尝试:
DateTime dtm1 = new DateTime(2012, 11, 15, 11, 3, 0);
DateTime dtm2 = new DateTime(2012, 11, 15, 11, 3, 20);
TimeSpan timespan3 = dtm2 - dtm1;
decimal _Hour = Convert.ToDecimal(timespan3.TotalHours);
什么时候输出如下:
_Hour = 0.00555555555555556M
而 which 不完全是,使用时是一种 double it 用于输出:
double _Hour = timespan3.TotalHours;
output: 0.0055555555555555549
一个例子:
public decimal tinhDienTichHinhThang(decimal D1, decimal D2, decimal H)
{
//tính tổng 2 đáy
decimal tong2Day = D1 + D2;
//cộng vào nhân chiều cao :))
tong2Day = tong2Day * H;
//return diện tích
return tong2Day / 2;
}
DateTime dtm1 = new DateTime(2012, 11, 15, 11, 3, 0);
DateTime dtm2 = new DateTime(2012, 11, 15, 11, 3, 20);
TimeSpan timespan3 = dtm2 - dtm1;
///progress
///cal1: _Hour
///cal2: decimal D1 = 0.25
///cal3: decimal D2 = 5
///cal4: decimal D3 = 0.9
decimal test1 = (decimal test1 = Math.Round((D1 + tinhDienTichHinhThang(D2, 0, Convert.ToDecimal(timespan3.TotalHours))) * D3, 3, MidpointRounding.AwayFromZero);
输出:test1 = 0.237
如果手动计算:
test1 = Math.Round((0.25 + ((5+0)*timespan3.TotalHours/2))*0.9, 3, MidpointRounding.AwayFromZero);
输出:test1 = 0.238(确切地说:0.2375)
注意:那么精确计算 win XP:0.2375 但是计算win 7则不准确。
请解释一下为什么会出现这个问题以及解决这个问题的方法?
【问题讨论】:
-
你到底想从什么开始?根据我对日期和时间的经验,如果您使用
double或decimal,您可能会以错误的方式处理事情。
标签: c#