在 C# 中,为什么舍入数学函数 Floor、Ceiling 和 Round 不返回int
?考虑到函数的结果始终是整数,为什么它返回一个float
, double
or decimal
?
double has the range of ±5.0 × 10−324 to ±1.7 × 10308 http://msdn.microsoft.com/en-us/library/678hzkk9.aspx and long has the range of –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 http://msdn.microsoft.com/en-us/library/ctetwysk.aspx. Unfortunately not all integral floating point values can be represented by an integer.
例如,1e19
已经超出了 64 位有符号整数的范围。
(long)Math.Round(1e19) == -9223372036854775808L // WTF?
虽然单参数确实过载Math.Round(double)
and Math.Round(decimal)
将始终返回整数值,这些重载仍然无法返回整数值类型。
If you know传递给函数的值将返回一个由整数值类型表示的值,您可以自行转换它。图书馆不会这样做,因为它需要考虑一般情况。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)