How do I convert a decimal to an int in C#?


Question

How do I convert a decimal to an int?

1
209
10/23/2012 11:56:25 PM

Use Convert.ToInt32 from mscorlib as in

decimal value = 3.14m;
int n = Convert.ToInt32(value);

See MSDN. You can also use Decimal.ToInt32. Again, see MSDN. Finally, you can do a direct cast as in

decimal value = 3.14m;
int n = (int) value;

which uses the explicit cast operator. See MSDN.

255
8/31/2009 1:20:11 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon