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

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

How do I convert a decimal to an int?

相关标签:
12条回答
  • 2020-12-02 14:19

    System.Decimal implements the IConvertable interface, which has a ToInt32() member.

    Does calling System.Decimal.ToInt32() work for you?

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-02 14:21

    You can't.

    Well, of course you could, however an int (System.Int32) is not big enough to hold every possible decimal value.

    That means if you cast a decimal that's larger than int.MaxValue you will overflow, and if the decimal is smaller than int.MinValue, it will underflow.

    What happens when you under/overflow? One of two things. If your build is unchecked (i.e., the CLR doesn't care if you do), your application will continue after the value over/underflows, but the value in the int will not be what you expected. This can lead to intermittent bugs and may be hard to fix. You'll end up your application in an unknown state which may result in your application corrupting whatever important data its working on. Not good.

    If your assembly is checked (properties->build->advanced->check for arithmetic overflow/underflow or the /checked compiler option), your code will throw an exception when an under/overflow occurs. This is probably better than not; however the default for assemblies is not to check for over/underflow.

    The real question is "what are you trying to do?" Without knowing your requirements, nobody can tell you what you should do in this case, other than the obvious: DON'T DO IT.

    If you specifically do NOT care, the answers here are valid. However, you should communicate your understanding that an overflow may occur and that it doesn't matter by wrapping your cast code in an unchecked block

    unchecked
    {
      // do your conversions that may underflow/overflow here
    }
    

    That way people coming behind you understand you don't care, and if in the future someone changes your builds to /checked, your code won't break unexpectedly.

    If all you want to do is drop the fractional portion of the number, leaving the integral part, you can use Math.Truncate.

    decimal actual = 10.5M;
    decimal expected = 10M;
    Assert.AreEqual(expected, Math.Truncate(actual));
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-02 14:21

    decimal d = 5.5;

    int i = decimal.ToInt32(d);// you will get i = 5
    

    ref: link text

    0 讨论(0)
  • 2020-12-02 14:22

    Rounding a decimal to the nearest integer

    decimal a ;
    int b = (int)(a + 0.5m);
    

    when a = 49.9, then b = 50

    when a = 49.5, then b = 50

    when a = 49.4, then b = 49 etc.

    0 讨论(0)
提交回复
热议问题