1

Is it possible to use Convert.ToInt32(double) and make it choose the smallest value?

I've read the examples in msdn, it converts a double to int using the closest value, which means that if i have a double equal to 2.9 it would set the int to 3.

Is it possible to use convert.toint32 and use 2?

ng80092b
  • 621
  • 1
  • 9
  • 24

2 Answers2

5

Use Math.Floor. See this link: https://msdn.microsoft.com/en-us/library/system.math.floor(v=vs.110).aspx

EDIT: Math.Floor returns a double, so you will have to cast it, such as int y = (int)Math.Floor(3.934333), which would return 3.

sovemp
  • 1,402
  • 1
  • 13
  • 31
  • I tried that before, but the math.floor didn't work, exactly because it returns a double and i need a int. What's the point of having a double "floored"? – ng80092b Feb 28 '15 at 04:08
  • You just cast the return of math.floor to an int. – sovemp Feb 28 '15 at 04:15
3

You can use just casting to int, you can check it:

double x = 2.9;
int y = (int) x;
Console.WriteLine (y); // 2