.net CInt 不会始终舍入 Double 值 - 如何删除小数部分?

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

CInt does not round Double value consistently - how can I remove the fractional part?

.netvb.netc#-to-vb.netrounding

提问by nbulba

I've stumbled upon an issue with CIntand converting a double to an integer.

我偶然发现了一个CInt问题并将双精度转换为整数。

The issue is the following:

问题如下:

CInt(10.5)  'Result is 10
CInt(10.51) 'Result it 11, but I expected 10...

I got used to C# style conversion where (int) 10.51is 10.

我习惯了 C# 风格的转换,哪里(int) 10.51是 10。

As pointed out in the question about Integer.Parse vs CInt, the result is just rounded in some fashion.

正如在关于Integer.Parse 与 CInt的问题中指出的那样,结果只是以某种方式四舍五入。

However, all I need is to get only integer part and throw away the fractional one. How can I achieve such type of conversion in VB.NET? After some research I see that I can use the Fix()function to do the trick, but is it the best choice?

但是,我所需要的只是获取整数部分并丢弃小数部分。我怎样才能在 VB.NET 中实现这种类型的转换?经过一番研究,我发现我可以使用该Fix()功能来实现这一目标,但它是最佳选择吗?

采纳答案by adatapost

You may use Intor Fixfunctions but return value type of these functions is double so you have to convert it to Integer if option strictis on.

您可以使用IntorFix函数,但这些函数的返回值类型是 double ,因此您必须将其转换为 Integer if option strictis on

  no = Convert.ToInt32(Int(10.51))

回答by Matt Wilko

Firstly, your assumption that CIntis equivalent to (int)in C# is incorrect.

首先,你的假设CInt相当于(int)在 C# 中是不正确的。

Secondly, the rounding behaviour of CIntis not randomly assigned - it actually uses "bankers rounding":

其次, 的舍入行为CInt不是随机分配的 - 它实际上使用“银行家舍入”:

Fractional Parts. When you convert a nonintegral value to an integral type, the integer conversion functions (CByte, CInt, CLng, CSByte, CShort, CUInt, CULng, and CUShort) remove the fractional part and round the value to the closest integer.

If the fractional part is exactly 0.5, the integer conversion functions round it to the nearest even integer. For example, 0.5 rounds to 0, and 1.5 and 2.5 both round to 2. This is sometimes called banker's rounding, and its purpose is to compensate for a bias that could accumulate when adding many such numbers together.

分数部分。将非整数值转换为整数类型时,整数转换函数(CByte、CInt、CLng、CSByte、CShort、CUInt、CULng 和 CUShort)会删除小数部分并将值四舍五入为最接近的整数。

如果小数部分正好是 0.5,整数转换函数会将其四舍五入到最接近的偶数整数。例如,0.5 舍入为 0,1.5 和 2.5 均舍入为 2。这有时称为银行家舍入,其目的是补偿在将许多此类数字相加时可能累积的偏差。

The best equivalent to using (int)in C# is the Fixfunction in the VisualBasicnamespace which rounds towards zero (same as Math.Truncate).

(int)在 C# 中使用的最佳等价物是命名空间中向零舍入的Fix函数VisualBasic(与 相同Math.Truncate)。

This however returns a Double value so you have to do a further conversion to get to your integer using CInt.

然而,这会返回一个 Double 值,因此您必须进行进一步的转换才能使用CInt.

CInt(Fix(10.5)) '10
CInt(Fix(10.51)) '10
CInt(Fix(11.5)) '11
CInt(Fix(-10.5)) '-10
CInt(Fix(-10.51)) '-10
CInt(Fix(-11.5)) '-11

回答by Ginka

I think you can try CInt(Math.Floor(10.51))hope this helps

我想你可以试试CInt(Math.Floor(10.51))希望这有帮助