如何四舍五入值 C # 到最接近的整数?

我想把 double 四舍五入到 int。

例如,

double a=0.4, b=0.5;

我想把它们都改成整数。

所以

int aa=0, bb=1;

aa来自 abb来自 b

有什么办法吗?

251347 次浏览

Math.Round

Rounds a double-precision floating-point value to the nearest integral value.

Check out Math.Round. You can then cast the result to an int.

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

Math.Round(0.5) returns zero due to floating point rounding errors, so you'll need to add a rounding error amount to the original value to ensure it doesn't round down, eg.

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();

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

Use a function in place of MidpointRounding.AwayFromZero:

myRound(1.11125,4)

Answer:- 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);
}

It is simple. So follow this code.

decimal d = 10.5;
int roundNumber = (int)Math.Floor(d + 0.5);

Result is 11

Another option:

string strVal = "32.11"; // will return 33
// string strVal = "32.00" // returns 32
// string strVal = "32.98" // returns 33


string[] valStr = strVal.Split('.');


int32 leftSide = Convert.ToInt32(valStr[0]);
int32 rightSide = Convert.ToInt32(valStr[1]);


if (rightSide > 0)
leftSide = leftSide + 1;




return (leftSide);

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?

Just some adjusting @BrunoLM's answer with more samples :

Math.Round(0.4); // =0
Math.Round(0.5); // =0
Math.Round(0.6); // =1


Math.Round(0.4, MidpointRounding.AwayFromZero); // = 0
Math.Round(0.5, MidpointRounding.AwayFromZero); // = 1
Math.Round(0.6, MidpointRounding.AwayFromZero); // = 1


Math.Round(0.4, MidpointRounding.ToEven); // = 0
Math.Round(0.5, MidpointRounding.ToEven); // = 0
Math.Round(0.6, MidpointRounding.ToEven); // = 1