c# - 为什么 C# Math.Floor() 返回 Double 而不是 Int

标签 c#

<分区>

Possible Duplicate:
Why does Math.Floor(Double) return a value of type Double?

为什么 C# Math.Floor() 返回 double 而不是 int

来自 MSDN 文档:

Returns the largest integer less than or equal to the specified double-precision floating-point number

它说它返回一个整数。可以返回 double,我总是可以将它转换为 int,但这很奇怪,不是吗?

最佳答案

并非如此,考虑到 double 可能比 int 大得多。您不希望 int 溢出 double 可能的大值。

只是为了告诉你我的意思:

Double.MaxValue = 1.7976931348623157E+308

Integer.MaxValue = 2,147,483,647

所以你可以有一个 3,000,000,000.50 的 double 并将它设为 floor,这会溢出 int 的最大值。

关于c# - 为什么 C# Math.Floor() 返回 Double 而不是 Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4130703/

相关文章:

c# - 如何将图像(可能是 RGB jpeg)转换为具有不规则调色板的 3 位颜色?

c# - 为 WinForm TreeView 控件实现向后导航和向前导航按钮功能

c# - .NET 中的序列化

c# - HttpListener不在主机外部监听吗?

c# - 从 WebRequestMethods.Ftp.ListDirectoryDe​​tails 中提取文件名

c# - 具有自定义 MembershipProvider 且没有 X.509 证书的 WCF,可能吗?

c# - C# 中的套接字重用有什么好处

c# - 'System.Net.ProtocolViolationException' {"You must write ContentLength bytes to the request stream before calling [Begin]GetResponse."}

c# - 如何访问维基百科

c# - NHibernate Validator 动态规则