C# 将 double 转换为 int,它是圆形还是只是去除数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13292148/
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
Casting a double as an int, does it round or just strip digits?
提问by cosmicsafari
Doing some calculations with doubles which then need to be cast to an int. So i have a quick question, when casting a double say 7.5 to an int, it will return 7.
使用双精度进行一些计算,然后需要将其转换为 int。所以我有一个简单的问题,当将 double 转换为 7.5 到 int 时,它将返回 7。
Is this a product of rounding or just striping anything after the decimal point?
这是四舍五入的产物还是只是在小数点后剥离任何内容?
If it is a product of rounding, is it smart ie 0.1 to 0.5 it rounds down and 0.6 to 0.9 it rounds up?
如果它是四舍五入的乘积,它是否聪明,即 0.1 到 0.5 向下舍入,0.6 到 0.9 向上舍入?
Cheers
干杯
采纳答案by Dennis Traub
It does not round, it just returns the integral part before the decimal point.
它不舍入,它只返回小数点前的整数部分。
Reference (thanks Rawling) Explicit Numeric Conversions Table:
When you convert a double or float value to an integral type, this value is rounded towards zero to the nearest integral value.
将 double 或 float 值转换为整数类型时,该值将向零舍入到最接近的整数值。
You can try simple issues like this by yourself by writing simple tests. The following test (using NUnit) will pass and therefore give an answer to your question:
您可以通过编写简单的测试自己尝试像这样的简单问题。以下测试(使用NUnit)将通过,因此可以回答您的问题:
[Test]
public void Cast_float_to_int_will_not_round_but_truncate
{
var x = 3.9f;
Assert.That((int)x == 3); // <-- This will pass
}
回答by Brian Warshaw
If it's returning 7 for a double of 7.5, then it isn't rounding, because rounding rules would dictate that anything 5 and above rounds up, not down.
如果它为 7.5 的双倍返回 7,则它不是四舍五入,因为四舍五入规则会规定任何 5 及以上的值都向上舍入,而不是向下舍入。
回答by msancho
It takes the integer part
它需要整数部分
double d = 0.9;
System.Console.WriteLine((int)d);
the result is 0
结果是 0
回答by Icemanind
Simply casting just strips everything past the decimal point. To round up or down, you can use the Math.Round() method. This will round up or down and provides a parameter on what to do if its midway. You could also use the Math.Floor()or Math.Ceiling()methods to implicitly round up or round down prior to casting. Here are some examples:
简单地强制转换只是去除小数点后的所有内容。要向上或向下舍入,您可以使用Math.Round() 方法。这将向上或向下取整,并提供一个参数,说明如果中途该做什么。您还可以使用Math.Floor()或Math.Ceiling()方法在转换之前隐式向上或向下舍入。这里有些例子:
double num1 = 3.5;
double num2 = 3.2;
double num3 = 3.9;
(int)num1 // returns 3;
(int)num2 // returns 3;
(int)num3 // returns 3 also;
(int)Math.Round(num1) // returns 4
(int)Math.Round(num2) // returns 3
(int)Math.Round(num3) // returns 4
(int)Math.Floor(num1) // returns 3
(int)Math.Floor(num2) // returns 3
(int)Math.Floor(num3) // returns 3
(int)Math.Ceiling(num1) // returns 4
(int)Math.Ceiling(num2) // returns 4;
(int)Math.Ceiling(num3) // returns 4;
回答by TylerOhlsen
Don't be fooled by assuming it rounds down. It strips the decimal off and purely returns the integer portion of the double. This is important with negative numbers because rounding down from 2.75 gives you 2, but rounding down from -2.75 give you -3. Casting does not round down so (int)2.75 gives 2, but (int)-2.75 gives you -2.
不要因为假设它四舍五入而被愚弄。它去除小数点并纯粹返回双精度数的整数部分。这对于负数很重要,因为从 2.75 向下舍入得到 2,但从 -2.75 向下舍入得到 -3。转换不会向下舍入,因此 (int)2.75 给出 2,但 (int)-2.75 给出 -2。
double positiveDouble = 2.75;
double negativeDouble = -2.75;
int positiveInteger = (int) positiveDouble;
int negativeInteger = (int) negativeDouble;
Console.WriteLine(positiveInteger + " = (int)" + positiveDouble);
Console.WriteLine(negativeInteger + " = (int)" + negativeDouble);
Console.ReadLine();
//Output: 2 = (int)2.75
// -2 = (int)-2.75
回答by Jeppe Stig Nielsen
From the C# Language Specification:
来自C# 语言规范:
In an
uncheckedcontext, the conversion always succeeds, and proceeds as follows.? If the value of the operand is NaN or infinite, the result of the conversion is an unspecified value of the destination type.
? Otherwise, the source operand is rounded towards zero to the nearest integral value. If this integral value is within the range of the destination type then this value is the result of the conversion.
? Otherwise, the result of the conversion is an unspecified value of the destination type.
在
unchecked上下文中,转换总是成功,并按如下方式进行。? 如果操作数的值为 NaN 或无穷大,则转换的结果是目标类型的未指定值。
? 否则,源操作数将向零舍入到最接近的整数值。如果此整数值在目标类型的范围内,则此值是转换的结果。
? 否则,转换的结果是目标类型的未指定值。
See also Explicit Numeric Conversions Table — Remarkson MSDN.
回答by 7heViking
A normal cast like this
像这样的普通演员
int number;
double decimals = 7.8987;
number = (int)decimals;
will return number = 7. That is because it just skips the least significant numbers. If you want it to round properly you can use Math.Round()like this:
将返回 number = 7。那是因为它只是跳过了最不重要的数字。如果您希望它正确舍入,您可以像这样使用Math.Round():
number = (int)Math.Round(number);
This will return number = 8.
这将返回 number = 8。

