> Decimal not exactly
Possible Duplicate:
What is the difference between Decimal, Float and Double in C#?
Help me.
I am developing a application in C# . I am trying:
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);
When do such me with output as follows:
_Hour = 0.00555555555555556M
and the which is not exactly, when using is a type of double it for output:
double _Hour = timespan3.TotalHours;
output: 0.0055555555555555549
One for example:
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);
output: test1 = 0.237
if calculate manual:
test1 = Math.Round((0.25 + ((5+0)*timespan3.TotalHours/2))*0.9, 3, MidpointRounding.AwayFromZero);
output:test1 = 0.238 (exactly: 0.2375)
Note: Calculate win XP then exactly: 0.2375 But calculate win 7 then not exactly.
Please interpret cho me the problem that why and resolve this problem with the way?
Doubles are floating point values. They are still limited to 64 bits of precision, but they have a value and an exponent that value is raised to (essentially scientific notation.) That's why they have more range than ints or longs, but because of this extended range they are unable to represent every possible value in that range.
Decimals have more bits than a double, so you're just seeing side effects of double's less precision compared to decimal when you are converting the value.
decimal test1 = (0.25M + 5M * Convert.ToDecimal(timespan3.TotalHours) / 2M) * 0.9M;
decimal test2 = (decimal)((0.25 + 5 * timespan3.TotalHours / 2) * 0.9);
double test3 = (0.25 + 5 * timespan3.TotalHours / 2) * 0.9;
When to perform do such they ra the following output not the same. You can guide to resolve this problem are no?
I'm not sure what is your problem. But if you have a problem with different values in decimal and double, it is by design. Numbers with floating point by default is not precize.
Update: (answer to your question in answers)
Due to this your question in answers, first and second statements different:
cause
5M * Convert.ToDecimal(timespan3.TotalHours)/ 2M
calculations is in decimal, but in second:
5 * timespan3.TotalHours / 2
calculations are in double (cause timespan3.TotalHours is double). It means that the result can be different from first statement. But at the and of calculation, you cast it to decimal and it implicitly convert value to decimal, that can change value.
In third statement, you have integer-valued calculations too, but double is floating point type, so it's value can be different from second statement, where fixed point type decimal.
链接地址: http://www.djcxy.com/p/21346.html下一篇: >小数不完全