为什么 Math.Floor(Double) 返回 Double 类型的值?

发布于 2024-08-03 01:55:34 字数 144 浏览 3 评论 0原文

我需要从小数或双精度数中获取左侧整数值。例如:我需要从 4.6 中获取值 4。我尝试使用 Math.Floor 函数,但它返回一个双精度值,例如:它从 4.6 返回 4.0。 MSDN 文档说它返回一个整数值。我在这里错过了什么吗?或者有不同的方法来实现我正在寻找的东西吗?

I need to get the left hand side integer value from a decimal or double. For Ex: I need to get the value 4 from 4.6. I tried using Math.Floor function but it's returning a double value, for ex: It's returning 4.0 from 4.6. The MSDN documentation says that it returns an integer value. Am I missing something here? Or is there a different way to achieve what I'm looking for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冰魂雪魄 2024-08-10 01:55:34

double 的范围比 intlong 的范围宽得多。考虑这段代码:

double d = 100000000000000000000d;
long x = Math.Floor(d); // Invalid in reality

整数超出了 long 的范围 - 那么您期望发生什么?

通常,您知道该值实际上intlong的范围内,因此您对其进行强制转换:

double d = 1000.1234d;
int x = (int) Math.Floor(d);

但该强制转换的责任是由开发人员负责,而不由 Math.Floor 本身负责。如果所有超出 long 范围的值都出现异常,那么它就会失败,这是不必要的限制。

The range of double is much wider than the range of int or long. Consider this code:

double d = 100000000000000000000d;
long x = Math.Floor(d); // Invalid in reality

The integer is outside the range of long - so what would you expect to happen?

Typically you know that the value will actually be within the range of int or long, so you cast it:

double d = 1000.1234d;
int x = (int) Math.Floor(d);

but the onus for that cast is on the developer, not on Math.Floor itself. It would have been unnecessarily restrictive to make it just fail with an exception for all values outside the range of long.

红衣飘飘貌似仙 2024-08-10 01:55:34

根据 MSDN,Math.Floor(double) 返回双精度: http://msdn .microsoft.com/en-us/library/e0b5f0xb.aspx

如果您希望将其作为 int:

int result = (int)Math.Floor(yourVariable);

我可以看到 MSDN 文章如何具有误导性,但他们应该指定虽然结果是“整数” (在本例中表示整数)它仍然是 TYPE Double

According to MSDN, Math.Floor(double) returns a double: http://msdn.microsoft.com/en-us/library/e0b5f0xb.aspx

If you want it as an int:

int result = (int)Math.Floor(yourVariable);

I can see how the MSDN article can be misleading, they should have specified that while the result is an "integer" (in this case meaning whole number) it is still of TYPE Double

仙女山的月亮 2024-08-10 01:55:34

如果您只需要数字的整数部分,请将数字转换为 int。这将截断小数点处的数字。

double myDouble = 4.6;
int myInteger = (int)myDouble;

If you just need the integer portion of a number, cast the number to an int. This will truncate the number at the decimal point.

double myDouble = 4.6;
int myInteger = (int)myDouble;
╰◇生如夏花灿烂 2024-08-10 01:55:34

Floor 将其保留为双精度,因此您可以用它进行更多双精度计算。
如果您希望将其作为 int,请将 Floor 的结果强制转换为 int。
不要将原始 double 转换为 int,因为对于负数,下限规则是不同的 (IIRC)。

Floor leaves it as a double so you can do more double calculations with it.
If you want it as an int, cast the result of floor as an int.
Don't cast the original double as an int because the rules for floor are different (IIRC) for negative numbers.

知你几分 2024-08-10 01:55:34
Convert.ToInt32(Math.Floor(Convert.ToDouble(value)))

如果您采用 4.6 它返回 4 作为输出,这将为您提供您想要的确切值。

Convert.ToInt32(Math.Floor(Convert.ToDouble(value)))

This will give you the exact value what you want like if you take 4.6 it returns 4 as output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文