C # rounding Algorithm

Source: Internet
Author: User

 

 

 

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.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

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

Code

/// <Summary>
/// Rounding data
/// </Summary>
/// <Param name = "v"> data to be processed </param>
/// <Param name = "x"> reserved decimal places </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 ));

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.