Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

This seems like it shoud be so obvious, I'm almost ashamed to ask.

 

I'm trying to cut off the remainder of a double by using CInt(valueOfDouble). Problem is, it's just rounding the number to the nearest integer instead of completely eliminating the remainder.

 

Please set me straight so I can get on with the business of banging my head against the wall.

 

Regards,

 

Mike

  • *Experts*
Posted

You converting it to an integer, thats just the way it works, rounding the number would be the most logical thing to do during a conversion like that. Btw., you shouldn't use CInt but System.Convert.ToInt32() :), as CInt is an old function which shouldn't exist by now :).

What you can do though is floor the number and convert it to an integer, or do the work yourself.

      Dim d as Double = 2.999999
      Dim i as integer = Convert.ToInt32(Math.Floor(d))
      

Posted
Btw.' date=' you shouldn't use CInt but System.Convert.ToInt32() :), as CInt is an old function which shouldn't exist by now :).[/quote']

There is no reason to avoid CInt. In fact, CInt is better for performance as well as being much less to type, because it avoids a function call.

 

Look here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vagrptypeconversion.asp

 

It starts out by saying right in the beginning:

These functions are compiled inline, meaning the conversion code is part of the code that evaluates the expression. Execution is faster because there is no call to a procedure to accomplish the conversion. Each function coerces an expression to a specific data type.

Posted

I had actually tried both conventions with the same results. I wasn't aware of putting math.floor in as part of the arguement though, I'll give it a try.

 

Thanks for the responses guys.

 

Regards,

 

MD

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...