how to convert from Double to Int32

B

Bob

System.Convert.ToInt32() doesn't work the way that I want, as the function
rounds the double value to the nearest 32-bit integer. For example, for a
number 11.75, it returns 12, but I want 11 (the floor). Is there a function
in .NET to do this?
 
M

Michael Lang

System.Convert.ToInt32() doesn't work the way that I want, as the
function rounds the double value to the nearest 32-bit integer. For
example, for a number 11.75, it returns 12, but I want 11 (the floor).
Is there a function in .NET to do this?

The class System.Math has a static Floor method. The floor method takes
and returns a double, so you have to cast it to integer afterwards.

double myDouble = 11.75;
int myInt = (int)Math.Floor(myDouble);

Michael Lang
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top