C # Rounding, top rounding, rounding down

Source: Internet
Author: User
Tags rounds

In dealing with some of the data, we want to be able to use the "rounding" method, but C # uses the "four six into 50% pairs" method, such as the following example, is the "four six into 50% pairs" results obtained:

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 achieve "rounding" in C #, I wrote the following function:

Code
     <summary>
Rounding method of implementing data
</summary>
<param name= "V" > Data to be processed </param>
<param name= "x" > Number of decimal digits reserved </param>
<returns> results after rounding </returns>
Private double Round (double v, int x)
{
BOOL isnegative = false;
If it's 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 be rounded up by the data.

Math.Round () has a problem rounding up:

Math.Round (2.5,0) = 2;

Math.Round (3.5,0) = 4;

2.5 should be equal to 3!

In the ASP also exists this problem, but ASP also has a formatnumber can use, but currently do not know how to use?

Explain:

Math.Round () accurately, this function is not rounded, but four six into the five, that is, less than 4 or greater than 6 of the admission is not controversial, and 5 is in the middle, if rounding will cause the overall deviation of the data, so the principle is: if the rounding bit is 5, The last digit after rounding is an even number, which is international practice.

Now do the project to 5 in, the workaround:

The current practice is to:

such as: (3.45*10+0.5) rounding, divided by 10

There is no rounding function in C #, in fact I know that the program language does not have rounding function, because rounding algorithm is unscientific, International is Banker rounding method Banker ' s rounding (banker rounding) algorithm, that is, four homes six into the five to take the pair. This is, in fact, the rounding standard set by the IEEE. So all IEEE compliant languages should be based on this algorithm

Math.Round method The default is also the Banker rounding method in. NET 2.0 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 a decimal value to the specified precision. The midpointrounding parameter that specifies how the value is rounded when one value is exactly in the middle of another two

This parameter is a MidpointRounding enumeration

There are two members of this enumeration:

Awayfromzero when a number is an intermediate value of another two digits, it is rounded to a value that has a greater absolute value in two values.

Toeven when a number is an intermediate value of another two digits, it is rounded to the nearest even number.

Therefore, to implement the rounding function, for positive numbers, you can add a Midpointrounding.awayfromzero parameter that specifies that when one number is the median of the other two digits it rounds to a value that is larger than the absolute value of two values, for example:

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

But for negative numbers, the above method is wrong.

So I need to write a function to handle it.

Double Chinaround (double value, int decimals)
{
if (Value < 0)
{
return Math.Round (value + 5/MATH.POW (Ten, decimals + 1), decimals, Midpointrounding.awayfromzero);
}
Else
{
return Math.Round (value, decimals, Midpointrounding.awayfromzero);
}
}

There are times when rounding is not necessary and may require rounding or rounding:

Math.ceiling () and Math.floor

Math.ceiling (3.1) = 4;
Math.floor (3.9) = 3;

The value of the sky plate and the floor value are not related to rounding. In fact, the result of floor is the same as (int), so you can write Math.floor (double) 2/3+0.5)

Floor and Ceil are functions in the math unit, and Uses math before use.

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

Floor directly to the small take, such as floor ( -123.55) =-124,floor (123.55) =123

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

Ceil directly to the large, such as Ceil ( -123.55) =-123, Ceil (123.55) =124

Round computed rounding, such as round ( -123.55) =-124,round (123.55) =124

C # rounding up the function to take the whole instance up

int a = 5;

int b = 2;

LbL. Text = convert.tostring (math.ceiling (Double) A/(double) b);

Http://www.cnblogs.com/sunney/archive/2010/07/28/1786903.html

C # Rounding, top rounding, rounding down

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.