The Method of Rounding Rounding Up and Rounding Down by C Programming

  • 2021-08-28 20:52:35
  • OfStack

In this paper, an example is given to describe the method of C # programming to realize 4 rounding and 5 incoming, upward and downward rounding. Share it for your reference, as follows:

When processing some data, we hope to use the "4-round-5-in" method, but C # adopts the method of "4-round-6-in-5-in-pairs", such as the following example, which is the result obtained by using "4-round-6-in-5-in-pairs":


double d1 = Math.Round(1.25, 1);//1.2
double d2 = Math.Round(1.24, 1);//1.2
double d3 = Math.Round(1.26, 1);//1.3
double d4 = Math.Round(1.35, 1);//1.4

In order to implement "4 rounds and 5 inputs" with C #, I wrote the following function:

Code


/// <summary>
///  To implement the data 4 Shed 5 Enter the law 
/// </summary>
/// <param name="v"> Data to be processed </param>
/// <param name="x"> Reserved decimal places </param>
/// <returns>4 Shed 5 The result of entering </returns>
private double Round(double v, int x)
{
  bool isNegative = false;
  // If it is negative 
  if (v < 0)
  {
   isNegative = true;
   v = -v;
  }
  int IValue = 1;
  for (int i = 1; i <= x; i++)
  {
   IValue = IValue * 10;
  }
  double Int = Math.Round(v * IValue + 0.5, 0);
  v = Int / IValue;
  if (isNegative)
  {
   v = -v;
  }
  return v;
}

After a simple test, the above function can realize the method of 4 rounding and 5 entering data.

Math. Round () has a problem with 4 rounding 5:


Math.Round(2.5,0) = 2; 
Math.Round(3.5,0) = 4;

2.5 should be equal to 3!

This problem also exists in ASP, but there is also an FormatNumber in ASP that can be used, but it is not known how to use it yet.

Explanation:

Math. Round () Accurately speaking, this function is not 4 rounding 5 inputs, but 4 rounding 6 inputs 5 pairs, that is to say, there is no dispute about the rounding of inputs less than 4 or greater than 6, and 5 is in the middle. If 4 rounding 5 inputs will cause the overall deviation of data, so the principle adopted is: If the rounding bit is 5, the last 1 bit after rounding is even, which is an international practice.

All the projects you do now need 5 inputs. The solution is as follows:

The current practice is:

For example: (3.45*10+0.5) rounded and divided by 10

There is no 4-round 5-input function in C #. In fact, all the programming languages I know don't have 4-round 5-input function, because the 4-round 5-input algorithm is unscientific. The internationally accepted algorithm is Banker rounding method Banker 's rounding (banker rounding) algorithm, that is, 4-round 6-input 5-take even. In fact, this is also the rounding standard stipulated in IEEE. Therefore, all languages conforming to IEEE standard should adopt this 1 algorithm

The Math. Round method also defaults to the Banker rounding method. In NET 2.0, the Math. Round method has several overloaded methods


Math.Round(Decimal, MidpointRounding) 
Math.Round(Double, MidpointRounding) 
Math.Round(Decimal, Int32, MidpointRounding) 
Math.Round(Double, Int32, MidpointRounding) 

Rounds the decimal value to the specified precision. The MidpointRounding parameter, which specifies how to round a value when it is just in the middle of the other two numbers

This parameter is an MidpointRounding enumeration

This enumeration has two members:

AwayFromZero When a number is the middle value of the other two numbers, it is rounded to the value with the greater absolute value of the two values.

ToEven When a number is the middle of the other two numbers, it is rounded to the nearest even number.

Therefore, to implement the 4-round 5-in function, for positive numbers, you can add 1 MidpointRounding. The AwayFromZero parameter specifies that when a number is the middle value of the other two numbers, it is rounded to the value with the greater absolute value of the two values. For example:

Math.Round(3.45, 2, MidpointRounding.AwayFromZero)

But for negative numbers, the above method is wrong again

So you need to write your own function to handle it


double ChinaRound(double value, int decimals) 
{ 
 if (value < 0) 
 { 
  return Math.Round(value + 5 / Math.Pow(10, decimals + 1), decimals, MidpointRounding.AwayFromZero); 
 } 
 else 
 { 
  return Math.Round(value, decimals, MidpointRounding.AwayFromZero); 
 } 
} 

Sometimes, if you don't have to use 4 rounds and 5 entries, you may need to round up or round down:

Math. Ceiling () and Math. Floor


Math.Ceiling(3.1)=4; 
Math.Floor(3.9)=3;

Taking the sky board value and the floor value has nothing to do with "4 houses and 5 entrances". In fact, the result of Floor is the same as that of (int), so it can also be written as Math. Floor ((double) 2/3 +0.5)

floor and ceil are functions in math unit, and Uses Math should be used first.

trunc and round are functions in system and unit and are available by default.

floor goes directly to smaller ones, such as floor (-123.55) =-124, floor (123.55) = 123

trunc directly cuts integers, such as trunc (-123.55) =-123, floor (123.55) = 123

ceil goes directly to larger ones, such as ceil (-123.55) =-123, ceil (123.55) = 124

round calculates 4 round 5 inputs, for example round (-123.55) =-124, round (123.55) = 124

C # Rounding Function Upward Rounding Instance


int a = 5; 
int b = 2; 
lbl.Text = Convert.ToString(Math.Ceiling((double)a / (double)b)); 

I hope this article is helpful to everyone's C # programming.


Related articles: