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

做~自己de王妃 提交于 2019-12-05 02:31:53

You may use Int or Fix functions but return value type of these functions is double so you have to convert it to Integer if option strict is on.

  no = Convert.ToInt32(Int(10.51))

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

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

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.

The best equivalent to using (int) in C# is the Fix function in the VisualBasic namespace which rounds towards zero (same as Math.Truncate).

This however returns a Double value so you have to do a further conversion to get to your integer using 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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!