C# - Rounding Down to Nearest Integer

前端 未结 5 785
抹茶落季
抹茶落季 2020-12-16 09:50

I have a C# app that is calculating some numbers. I need to round down.

var increment = 1.25;
var result = 50.45 - 23.70;    // equals 26.75
int interval = d         


        
相关标签:
5条回答
  • 2020-12-16 10:13

    Just try this..

     int interval = Convert.ToInt32(Math.Floor(different/increment));
    
    0 讨论(0)
  • 2020-12-16 10:20

    You can also just simply cast the result to int. This will truncate the number.

    int interval = (int)(difference / increment);
    
    0 讨论(0)
  • 2020-12-16 10:20
    Convert.ToSingle(); 
    

    is another method

    0 讨论(0)
  • 2020-12-16 10:22

    Use the static Math class:

    int interval = (int)Math.Floor(difference/increment);
    

    Math.Floor() will round down to the nearest integer.

    0 讨论(0)
  • 2020-12-16 10:23

    The Math.Floor() function should do the trick:

    int interval = (int)Math.Floor(difference / increment);
    

    See also: https://msdn.microsoft.com/de-de/library/e0b5f0xb%28v=vs.110%29.aspx

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