SoFunction
Updated on 2025-03-06

C# Rounding method using round function

This article describes the method of rounding C# using round function. Share it for your reference. The specific analysis is as follows:

The round function in C# is actually not a real rounding function. The round function in general programming languages ​​is not a rounding function, but a banker rounding method function, that is, "rounding six into five is considered, if five is not zero is added, if five is zero is added, if five is zero is considered, odd or even is taken, five is before even, five is before odd, one is added"

But the round function in C# does not seem to follow this rule completely. Let’s take a look at the examples given by Microsoft:

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)
     ("{0} --> {1}", value,
  (value, 2, ));
  }
}
// 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

I saw it. After doing round operations of 2.135 and 3.135, the result was that 2.135 did not carry, and 2.145 carried.

I hope this article will be helpful to everyone's C# programming.