Many people have told me the same, only know Math.Round () in C # is used for rounding decimal places specified retention
But in fact it is not really a rounding, but bankers algorithm rounded banker to take even
In fact this is the IEEE specification, all IEEE-compliant languages should adopt such an algorithm.
Its rules are as follows:
1, when the truncated value is less than 5 bits. Direct rounding
2, when rounded down value is greater than 6 bits, add with carry 1
3, when the rounding-bit value equal to 5, two cases:
(1) If there is other than 0 (i.e., not the last one 5) 5 back, add with carry 1
(2) only if the rear 0 5 (i.e., the last one is 5), then 5 judges according to the parity of the former, the former is an odd number plus a carry, is even the truncated
5 situation encountered need to lay down the only one, is the need to retain the last 5 valid number after the precision and former number is even
The higher the number of precision, it is more like the real algorithm rounding
When we use this function, for digital processing are usually those with n decimal digits, and we are usually used to display it was only 2-4, so this is also not easy to find the problem
We may not understand just text, write a few examples below
Math.Round ( 1.14 , 1 ) // the Result: 1.1 Math.Round ( 1.25 , 1 ) // the Result: 1 .2 Five is the last and the former is even, also lay down Math.Round ( 1.15 , 1 ) // the Result: 1 .2 five is the last but the first bit is odd, plus a carry Math.Round ( 1.16 , 1 ) // the Result: 1 .2
.NET 2.0 start, Math.Round method provides an enumeration MidpointRounding.AwayFromZero option can be used to achieve the "rounding" in the traditional sense.
Math.Round(1.25 , 1) //result:1 .2 Math.Round(1.25 , 1, MidpointRounding.AwayFromZero) //result:1 .3