How to round up value C# to the nearest integer?

后端 未结 9 1986
慢半拍i
慢半拍i 2020-11-28 09:02

I want to round up double to int.

Eg,

double a=0.4, b=0.5;

I want to change them both to integer.

so that

         


        
相关标签:
9条回答
  • 2020-11-28 09:33

    It is also possible to round negative integers

    // performing d = c * 3/4 where d can be pos or neg
    d = ((c * a) + ((c>0? (b>>1):-(b>>1)))) / b;
    // explanation:
    // 1.) multiply:          c * a  
    // 2.) if c is negative:  (c>0? subtract half of the dividend 
    //                              (b>>1) is bit shift right = (b/2)
    //     if c is positive:  else  add half of the dividend 
    // 3.) do the division
    // on a C51/52 (8bit embedded) or similar like ATmega the below code may execute in approx 12cpu cycles (not tested)
    

    Extended from a tip somewhere else in here. Sorry, missed from where.

    /* Example test: integer rounding example including negative*/
    #include <stdio.h>
    #include <string.h>
    
    int main () {
       //rounding negative int
       // doing something like d = c * 3/4
       int a=3;
       int b=4;
       int c=-5;
       int d;
       int s=c;
       int e=c+10;
    
    
       for(int f=s; f<=e; f++) {
          printf("%d\t",f);
    
          double cd=f, ad=a, bd=b , dd;
    
          // d = c * 3/4  with double
          dd = cd * ad / bd;
    
          printf("%.2f\t",dd);
          printf("%.1f\t",dd);        
          printf("%.0f\t",dd);
    
          // try again with typecast have used that a lot in Borland C++ 35 years ago....... maybe evolution has overtaken it ;) ***
          // doing div before mul on purpose
          dd =(double)c * ((double)a / (double)b);
          printf("%.2f\t",dd);
    
          c=f;
          // d = c * 3/4  with integer rounding
          d = ((c * a) + ((c>0? (b>>1):-(b>>1)))) / b;
          printf("%d\t",d);
          puts("");
      }
     return 0;
    }
    
    /* test output
    in  2f     1f   0f cast int   
    -5  -3.75   -3.8    -4  -3.75   -4  
    -4  -3.00   -3.0    -3  -3.75   -3  
    -3  -2.25   -2.2    -2  -3.00   -2  
    -2  -1.50   -1.5    -2  -2.25   -2  
    -1  -0.75   -0.8    -1  -1.50   -1  
     0   0.00    0.0     0  -0.75    0  
     1   0.75    0.8     1   0.00    1  
     2   1.50    1.5     2   0.75    2  
     3   2.25    2.2     2   1.50    2  
     4   3.00    3.0     3   2.25  3    
     5   3.75    3.8     4   3.00   
    
    // by the way evolution: 
    // Is there any decent small integer library out there for that by now?
    
    0 讨论(0)
  • 2020-11-28 09:38

    The .NET framework uses banker's rounding in Math.Round by default. You should use this overload:

    Math.Round(0.5d, MidpointRounding.AwayFromZero)  //1
    Math.Round(0.4d, MidpointRounding.AwayFromZero)  //0
    
    0 讨论(0)
  • 2020-11-28 09:39

    Use Math.Ceiling to round up

    Math.Ceiling(0.5); // 1
    

    Use Math.Round to just round

    Math.Round(0.5, MidpointRounding.AwayFromZero); // 1
    

    And Math.Floor to round down

    Math.Floor(0.5); // 0
    
    0 讨论(0)
提交回复
热议问题