[c#] 값 C #을 가장 가까운 정수로 반올림하는 방법은 무엇입니까?

double을 int로 반올림하고 싶습니다.

예 :

double a=0.4, b=0.5;

둘 다 정수로 변경하고 싶습니다.

그래서

int aa=0, bb=1;

aa은 (는)에서 a이며 bb에서 왔습니다 b.

그렇게 할 공식이 있습니까?



답변

사용 Math.Ceiling라운드 최대

Math.Ceiling(0.5); // 1

사용 Math.Round다만 라운드

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

그리고 Math.Floor내림

Math.Floor(0.5); // 0


답변

Math.Round를 확인하십시오 . 그런 다음 결과를 int.


답변

.NET 프레임 워크는 Math.Round기본적으로 은행원의 반올림을 사용합니다 . 이 오버로드를 사용해야합니다.

Math.Round(0.5d, MidpointRounding.AwayFromZero)  //1
Math.Round(0.4d, MidpointRounding.AwayFromZero)  //0


답변

대신 함수를 사용하십시오 MidpointRounding.AwayFromZero.

myRound(1.11125,4)

답변 :-1.1114

public static Double myRound(Double Value, int places = 1000)
{
    Double myvalue = (Double)Value;
    if (places == 1000)
    {
        if (myvalue - (int)myvalue == 0.5)
        {
            myvalue = myvalue + 0.1;
            return (Double)Math.Round(myvalue);
        }
        return (Double)Math.Round(myvalue);
        places = myvalue.ToString().Substring(myvalue.ToString().IndexOf(".") + 1).Length - 1;
    } if ((myvalue * Math.Pow(10, places)) - (int)(myvalue * Math.Pow(10, places)) > 0.49)
    {
        myvalue = (myvalue * Math.Pow(10, places + 1)) + 1;
        myvalue = (myvalue / Math.Pow(10, places + 1));
    }
    return (Double)Math.Round(myvalue, places);
}


답변

수학 라운드

배정 밀도 부동 소수점 값을 가장 가까운 정수 값으로 반올림합니다.


답변

Math.Round (0.5)는 부동 소수점 반올림 오류로 인해 0을 반환하므로 반올림하지 않도록 원래 값에 반올림 오류 양을 추가해야합니다.

Console.WriteLine(Math.Round(0.5, 0).ToString()); // outputs 0 (!!)
Console.WriteLine(Math.Round(1.5, 0).ToString()); // outputs 2
Console.WriteLine(Math.Round(0.5 + 0.00000001, 0).ToString()); // outputs 1
Console.WriteLine(Math.Round(1.5 + 0.00000001, 0).ToString()); // outputs 2
Console.ReadKey();


답변

음의 정수를 반올림 할 수도 있습니다.

// 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)

여기 어딘가의 팁에서 확장되었습니다. 죄송합니다. 어디에서 놓쳤습니다.

/* 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?