在 C# 中将 double 转换为 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10754251/
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-09 15:08:03  来源:igfitidea点击:

Converting a double to an int in C#

c#intdouble

提问by Wouter Dorgelo

In our code we have a double that we need to convert to an int.

在我们的代码中,我们需要将 double 转换为 int。

double score = 8.6;
int i1 = Convert.ToInt32(score);
int i2 = (int)score;

Can anyone explain me why i1 != i2?

谁能解释我为什么i1 != i2

The result that I get is that: i1 = 9and i2 = 8.

我得到的结果是:i1 = 9i2 = 8

采纳答案by Jon

Because Convert.ToInt32rounds:

因为Convert.ToInt32回合:

Return Value: rounded to the nearest 32-bit signed integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is converted to 4, and 5.5 is converted to 6.

返回值:四舍五入到最接近的 32 位有符号整数。如果 value 介于两个整数之间,则返回偶数;即4.5转4,5.5转6。

...while the cast truncates:

...而演员截断

When you convert from a double or float value to an integral type, the value is truncated.

当您从 double 或 float 值转换为整数类型时,该值将被截断。

Update:See Jeppe Stig Nielsen's comment below for additional differences (which however do not come into play if scoreis a real number as is the case here).

更新:有关其他差异,请参阅下面 Jeppe Stig Nielsen 的评论(但是,如果score是实数,则不会发挥作用,就像这里的情况一样)。

回答by Jon

ToInt32 rounds. Casting to int just throws away the non-integer component.

ToInt32 轮。转换为 int 只会丢弃非整数组件。

回答by David

you can round your double and cast ist:

你可以舍入你的 double 和 cast :

(int)Math.Round(myDouble);

回答by neilgmacy

Casting will ignore anything after the decimal point, so 8.6 becomes 8.

转换将忽略小数点后的任何内容,因此 8.6 变为 8。

Convert.ToInt32(8.6)is the safe way to ensure your double gets rounded to the nearest integer, in this case 9.

Convert.ToInt32(8.6)是确保双精度舍入到最接近的整数(在本例中为 9)的安全方法。

回答by Evdzhan Mustafa

In the provided example your decimal is 8.6. Had it been 8.5 or 9.5, the statement i1 == i2might have been true. Infact it would have been true for 8.5, and false for 9.5.

在提供的示例中,您的小数点是8.6。如果是 8.5 或 9.5,则i1 == i2语句 可能是正确的。事实上,它对于 8.5 是正确的,对于 9.5 是错误的。

Explanation:

解释:

Regardless of the decimal part, the second statement, int i2 = (int)scorewill discard the decimal part and simply return you the integer part. Quite dangerous thing to do, as data loss might occur.

无论小数部分如何,第二条语句int i2 = (int)score将丢弃小数部分并简单地返回整数部分。这样做很危险,因为可能会发生数据丢失。

Now, for the first statement, two things can happen. If the decimal part is 5, that is, it is half way through, a decision is to be made. Do we round up or down? In C#, the Convert class implements banker's rounding. See thisanswer for deeper explanation. Simply put, if the number is even, round down, if the number is odd, round up.

现在,对于第一个语句,可能会发生两件事。如果小数部分是5,也就是已经过了一半,就要做出决定了。我们是向上还是向下取整?在 C# 中,Convert 类实现了银行家的舍入。请参阅答案以获得更深入的解释。简单地说,如果数字是偶数,则向下取整,如果数字是奇数,则向上取整。

E.g. Consider:

例如考虑:

        double score = 8.5;
        int i1 = Convert.ToInt32(score); // 8
        int i2 = (int)score;             // 8

        score += 1;
        i1 = Convert.ToInt32(score);     // 10
        i2 = (int)score;                 // 9