我今天过得很艰难,但有些事情不太对劲。
在我的 C# 代码中,我有这样的内容:
Math.Ceiling((decimal)(this.TotalRecordCount / this.PageSize))
Where (int)TotalRecordCount
= 12 和(int)PageSize
= 5.我得到的结果是2。
(两个值都是int
值。)
根据我的计算,12 / 5 = 2.4。我以为 Math.Ceiling 总是会向上取整,在这种情况下,给我 3?
PS,如果我这样做:
Math.Ceiling(this.TotalRecordCount / this.PageSize)
我收到消息:
Math.Ceiling(this.TotalRecordCount / this.PageSize)
以下方法或属性之间的调用是不明确的:
“System.Math.Ceiling(十进制)”和“System.Math.Ceiling(双精度)”
您会看到“向下舍入”,因为截断发生在到达之前Math.Ceiling
.
当你这样做时
(this.TotalRecordCount / this.PageSize)
它是整数除法,其结果是截断的int
;现在已经太晚了decimal
.
要解决此问题,请在除法之前进行强制转换:
Math.Ceiling(((decimal)this.TotalRecordCount / this.PageSize))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)