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

后端 未结 12 2447
再見小時候
再見小時候 2020-12-02 13:47

How do I convert a decimal to an int?

12条回答
  •  甜味超标
    2020-12-02 14:20

    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.

提交回复
热议问题