Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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.51 is 10.

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

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?

like image 404
nbulba Avatar asked Oct 07 '11 07:10

nbulba


1 Answers

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))
like image 122
KV Prajapati Avatar answered Oct 31 '22 17:10

KV Prajapati