Home > Backend Development > C#.Net Tutorial > C# rounding round function use

C# rounding round function use

大家讲道理
Release: 2016-11-11 13:45:09
Original
3416 people have browsed it

The round function in C# is not actually a real rounding function, and the round function in general programming languages ​​is not a rounding function, but a banker's rounding method function, that is, "round up to five, then five is not If zero is zero, add one. If the last five is zero, look at the odd or even number. If the first five is even, discard it. If the first five is odd, add one."

using System;
  
public class Example
{
   public static void Main()
   {
      double[] values = { 2.125, 2.135, 2.145, 3.125, 3.135, 3.145 };
      foreach (double value in values)
         Console.WriteLine("{0} --> {1}", value,
                           Math.Round(value, 2, MidpointRounding.AwayFromZero));
  
   }
}
// The example displays the following output:
//       2.125 --> 2.13
//       2.135 --> 2.13
//       2.145 --> 2.15
//       3.125 --> 3.13
//       3.135 --> 3.14
//       3.145 --> 3.15
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template