【发布时间】:2014-09-24 07:53:48
【问题描述】:
我最近将一个属性从 double 更改为 decimal 并发现了一个奇怪的效果。 该属性返回的所有数字都被截断。
这是部分代码:
private int _SalesPrice;
// [ ... ]
public decimal SalesPrice
{
get { return Convert.ToDecimal(_SalesPrice / 1000); }
set { _SalesPrice = Convert.ToInt32(value * 1000); }
}
如果我的代码中有这样的内容:
SalesPrice = 5.98
_SalesPrice 将为 5980,但 SalesPrice 返回不带小数的 5。
这背后的原因是什么? 使用 double 而不是 decimal 效果很好。
我正在使用 VS2010
【问题讨论】:
-
int a = 5980/1000 = 5
-
十进制 a = 5980/1000 = 5
-
return (decimal)_SalesPrice / 1000; -
Using double instead of decimal works just fine.- 很确定它没有。 WTF 还对所有答案投了反对票?
标签: c# decimal truncation