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

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

How do I convert a decimal to an int?

12条回答
  •  天命终不由人
    2020-12-02 14:21

    A neat trick for fast rounding is to add .5 before you cast your decimal to an int.

    decimal d = 10.1m;
    d += .5m;
    int i = (int)d;
    

    Still leaves i=10, but

    decimal d = 10.5m;
    d += .5m;
    int i = (int)d;
    

    Would round up so that i=11.

提交回复
热议问题