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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1348892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:29:45  来源:igfitidea点击:

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

c#mathfloor

提问by

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?

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

采纳答案by Jon Skeet

The range of doubleis much wider than the range of intor long. Consider this code:

的范围doubleint或的范围宽得多long。考虑这个代码:

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?

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

Typically you know that the value will actuallybe within the range of intor long, so you cast it:

通常,您知道该值实际上将在intor的范围内long,因此您可以对其进行转换:

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

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

但是这个演员的责任在于开发者,而不是它Math.Floor自己。将long.

回答by Neil N

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

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

If you want it as an int:

如果你想要它作为一个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

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

回答by Jon Onstott

That is correct. Looking at the declaration, Math.Floor(double) yields a double (see http://msdn.microsoft.com/en-us/library/e0b5f0xb.aspx). I assume that by "integer" they mean "whole number".

那是正确的。查看声明,Math.Floor(double) 产生一个 double(参见http://msdn.microsoft.com/en-us/library/e0b5f0xb.aspx)。我认为“整数”是指“整数”。

回答by plinth

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.

Floor 将其保留为 double,因此您可以使用它进行更多的 double 计算。如果你想要它作为一个整数,将 floor 的结果转换为一个整数。不要将原始 double 转换为 int,因为对于负数,floor 的规则是不同的 (IIRC)。

回答by Jon Seigel

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.

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

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

回答by user3306645

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

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

如果您将4.64作为输出返回,这将为您提供您想要的确切值。