In C, the result of Math.Round(2.5) is 2.

It's supposed to be three, isn't it? Why change to 2 in C?

### #1 building

I ran into this problem, my SQL Server didn't round the C application to 0.5 to 1. So you will see two different results.

This is an implementation with int / long. This is how Java rounds.

`int roundedNumber = (int)Math.Floor(d + 0.5);`

This is probably the most effective way you might think of.

If you want to keep it double and use decimal precision, you actually only need to use an exponent of 10 based on the number of decimal places.

`public double getRounding(double number, int decimalPoints) { double decimalPowerOfTen = Math.Pow(10, decimalPoints); return Math.Floor(number * decimalPowerOfTen + 0.5)/ decimalPowerOfTen; }`

You can enter a negative decimal for the decimal point, and its word is also very good.

`getRounding(239, -2) = 200`

### #2 building

It's ugly, but it always produces the right arithmetic rounding.

`public double ArithRound(double number,int places){ string numberFormat = "###."; numberFormat = numberFormat.PadRight(numberFormat.Length + places, '#'); return double.Parse(number.ToString(numberFormat)); }`

### #3 building

This is the solution I have to solve:

`Public Function Round(number As Double, dec As Integer) As Double Dim decimalPowerOfTen = Math.Pow(10, dec) If CInt(number * decimalPowerOfTen) = Math.Round(number * decimalPowerOfTen, 2) Then Return Math.Round(number, 2, MidpointRounding.AwayFromZero) Else Return CInt(number * decimalPowerOfTen + 0.5) / 100 End If End Function`

Trying to use 1.905 with two decimal points will get the expected 1.91, but math. Round (1.905,2, midpointrouting. Awayfromzero) will get 1.90! For most basic problems that programmers may encounter, the Math.Round method is absolutely inconsistent and unusable. I have to check (int) 1.905 * decimalPowerOfTen = Math.Round(number * decimalPowerOfTen, 2) so I don't want to round.

### #4 building

The default rounding of midpointrouting.toeven or Bankers (2.5 becomes 2, 4.5 becomes 4, and so on) is a headache for me before I write the accounting report, so I will write something I found before and what I found from it to enter the position.

### Who are these bankers taking even numbers (maybe British bankers!)?

From wikipedia

The origin of banker rounding is still obscure. If this rounding method used to be the standard of banking, it turns out to be extremely difficult to find evidence. On the contrary, Section 2 of the European Commission's report "Introduction to the euro and rounding of currencies" shows that there was no standard rounding method for banks before. It specifies that the "half way" amount should be rounded.

Especially for banks, this seems like a very strange rounding method, unless the bank will, of course, use a large amount of deposits to collect even amounts. Deposit £ 2.4m, but we call it £ 2m.

IEEE 754 standard can be traced back to 1985, and gives two rounding methods, but the standard recommends banker standard. This article Wikipedia articles A long list of how languages are rounded (correct me if any of the following errors) and most of them don't use Bankers' but are rounded at school:

- C / C ++ round() in math.h is rounded to zero (not bankers)
- Java Math.Round Into zero. House Enter the result, add the result to 0.5, and cast it to an integer). In BigDecimal There is another option
- Perl Use a similar approach to C
- Javascript is the same as Math.Round in Java.

### #5 building

Silverlight does not support the midpointrouting option. This is an extension of Silverlight, which adds the midpointrouting enumeration:

`public enum MidpointRounding { ToEven, AwayFromZero } public static class DecimalExtensions { public static decimal Round(this decimal d, MidpointRounding mode) { return d.Round(0, mode); } /// <summary> /// Rounds using arithmetic (5 rounds up) symmetrical (up is away from zero) rounding /// </summary> /// <param name="d">A Decimal number to be rounded.</param> /// <param name="decimals">The number of significant fractional digits (precision) in the return value.</param> /// <returns>The number nearest d with precision equal to decimals. If d is halfway between two numbers, then the nearest whole number away from zero is returned.</returns> public static decimal Round(this decimal d, int decimals, MidpointRounding mode) { if ( mode == MidpointRounding.ToEven ) { return decimal.Round(d, decimals); } else { decimal factor = Convert.ToDecimal(Math.Pow(10, decimals)); int sign = Math.Sign(d); return Decimal.Truncate(d * factor + 0.5m * sign) / factor; } } }`

Source: http : //anderly.com/2009/08/08/silverlight-midpoint-rounding-solution/