When processing some data, we hope to use the "Rounding" method, but C # uses the "four homes, six homes, five into two" method, as shown in the following example, the result is obtained using "four homes, six homes, and five into two:

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

To use C # To implement "Rounding", I wrote the following function:

Code

/// <Summary> /// implement the Rounding Method of Data /// </Summary> /// <Param name = "v"> data to be processed </Param >/// <Param name = "X"> Number of decimals retained </param> /// <returns> result after rounding </returns> private double round (Double V, int X) {bool isnegative = false; // if it is a negative number, 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 rounding method of data.

Math. Round () has a problem in rounding:

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 a formatnumber in ASP that can be used, but I do not know how to use it yet?

Explanation:

Math. round, while 5 is in the center, if rounding, it will cause the overall data deviation. Therefore, the principle is: if the rounding bit is 5, the last digit after rounding is an even number, this is an international practice.

Currently, five projects are required. solution:

The current practice is:

For example: (3.45*10 + 0.5) Get an integer and divide it by 10.

In C #, there is no rounding function. In fact, all programming languages I know do not have rounding functions. Because the rounding algorithm is not scientific, the banker Rounding Method banker's rounding (banker rounding) is adopted internationally) algorithm, that is, four homes, six in five get even. In fact, this is also the IEEE Standard for rounding. Therefore, all IEEE-compliant languages should adopt this algorithm.

The math. Round method defaults to the banker Rounding Method in. NET 2.0. The math. Round method has several overload methods.

Math. Round (decimal, midpointrounding)

Math. Round (double, midpointrounding)

Math. Round (decimal, int32, midpointrounding)

Math. Round (double, int32, midpointrounding)

Rounds a small value to a specified precision. The midpointrounding parameter specifies how to round a value when it is in the middle of the other two numbers

This parameter is a midpointrounding enumeration.

This enumeration has two members:

Awayfromzero when a number is the median of the other two numbers, it is rounded to a value with a larger absolute value.

Toeven when a number is the median of the other two numbers, it is rounded to the nearest even number.

Therefore, to implement a rounding function, you can add a midpointrounding for a positive number. the awayfromzero parameter specifies that when a number is the center of the other two numbers, it is rounded to a value with a large absolute value in the two values. For example:

Math. Round (3.45, 2, midpointrounding. awayfromzero)

However, the method above the negative number is incorrect.

Therefore, you need to write a function for processing.

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

}

}

In some cases, you may not have to rounding it up or down:

Math. Ceiling () and math. Floor

Math. Ceiling (3.1) = 4;

Math. Floor (3.9) = 3;

The day plate value has nothing to do with the floor value. In fact, the result of floor is the same as that of (INT), so you can also write math. Floor (double) 2/3 + 0.5)

Floor and ceil are functions in math unit. Uses math is required before use.

Trunc and round are functions in system unit and can be used by default.

The floor is directly retrieved from a small value, such as floor (-123.55) =-124, floor (123.55) = 123

Trunc cut down integers directly, such as trunc (-123.55) =-123, floor (123.55) = 123

Ceil is directly retrieved from large values, such as Ceil (-123.55) =-123, Ceil (123.55) = 124

Round calculates rounding, for example, round (-123.55) =-124, round (123.55) = 124

C # Use the integer function to get the whole instance up.

Int A = 5;

Int B = 2;

LBL. Text = convert. tostring (math. Ceiling (double) A/(double) B ));

**************************************** **************************************** **************************************** ********