C # rounding up, rounded up, and rounded down

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

Related Article

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.