gpt4 book ai didi

c# - 转换 double --> 不完全是十进制

转载 作者:太空宇宙 更新时间:2023-11-03 13:50:19 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
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

而且不完全是,当使用时是一种双倍的输出类型:

 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就不太准确了。

请给我解释一下为什么会出现这个问题,并用方法解决这个问题?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com