Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the VB.NET method corresponding to the old CInt() VB method?

I don't want use old Visual Basic methods in my code, and I'm confused about what's the newest VB.NET method corresponding to the old CInt() Visual Basic method.

For example,

Dim n1 as Double : n1 = CInt(2.1111111) 'Gets only 2 without rounding it
Dim n2 as Double : n2 = CInt(2.7777777) 'Get only 2 without rounding it
like image 216
Max Avatar asked Oct 29 '25 08:10

Max


1 Answers

You can use CInt in VB.NET as well. It is a standard type conversion function supported by the VB.NET compiler. There is no reason to avoid it.

However, nearly every type conversion operation will round to the nearest integer (including other methods such as Convert.ToInt32, etc.), not truncate. You can force a truncation by using Int or Fix (which have different behaviors for negative numbers).

Note that, you can do the same as Int/Fix by using Math.Truncate and Math.Floor, should you wish to avoid the VB.NET specific conversion routines.


On a side note - CInt in Visual Basic 6.0 also performed rounding. The behavior in VB.NET for CInt is the same as in Visual Basic 6.0.

like image 52
Reed Copsey Avatar answered Oct 31 '25 13:10

Reed Copsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!