当前位置: 首页>>代码示例>>C#>>正文


C# Math.Round方法代码示例

本文整理汇总了C#中System.Math.Round方法的典型用法代码示例。如果您正苦于以下问题:C# Math.Round方法的具体用法?C# Math.Round怎么用?C# Math.Round使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Math的用法示例。


在下文中一共展示了Math.Round方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: foreach

decimal[] values = { 1.15m, 1.25m, 1.35m, 1.45m, 1.55m, 1.65m };
decimal sum = 0;

// Calculate true mean.
foreach (var value in values)
   sum += value;

Console.WriteLine("True mean:     {0:N2}", sum/values.Length);

// Calculate mean with rounding away from zero.
sum = 0;
foreach (var value in values)
   sum += Math.Round(value, 1, MidpointRounding.AwayFromZero);

Console.WriteLine("AwayFromZero:  {0:N2}", sum/values.Length);

// Calculate mean with rounding to nearest.
sum = 0;
foreach (var value in values)
   sum += Math.Round(value, 1, MidpointRounding.ToEven);

Console.WriteLine("ToEven:        {0:N2}", sum/values.Length);
开发者ID:.NET开发者,项目名称:System,代码行数:22,代码来源:Math.Round

输出:

True mean:     1.40
AwayFromZero:  1.45
ToEven:        1.40

示例2: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("{0,5} {1,20:R}  {2,12} {3,15}\n", 
                        "Value", "Full Precision", "ToEven",
                        "AwayFromZero");
      double value = 11.1;
      for (int ctr = 0; ctr <= 5; ctr++)    
         value = RoundValueAndAdd(value);

      Console.WriteLine();

      value = 11.5;
      RoundValueAndAdd(value);
   }
   
   private static double RoundValueAndAdd(double value)
   {
      const double tolerance = 8e-14;

      Console.WriteLine("{0,5:N1} {0,20:R}  {1,12} {2,15}", 
                        value, 
                        RoundApproximate(value, 0, tolerance, MidpointRounding.ToEven),
                        RoundApproximate(value, 0, tolerance, MidpointRounding.AwayFromZero));
      return value + .1;
   }

   private static double RoundApproximate(double dbl, int digits, double margin, 
                                     MidpointRounding mode)
   {                                      
      double fraction = dbl * Math.Pow(10, digits);
      double value = Math.Truncate(fraction); 
      fraction = fraction - value;   
      if (fraction == 0)
         return dbl;
      
      double tolerance = margin * dbl;
      // Determine whether this is a midpoint value.
      if ((fraction >= .5 - tolerance) & (fraction <= .5 + tolerance)) {
         if (mode == MidpointRounding.AwayFromZero)
            return (value + 1)/Math.Pow(10, digits);
         else
            if (value % 2 != 0)
               return (value + 1)/Math.Pow(10, digits);
            else
               return value/Math.Pow(10, digits);
      }
      // Any remaining fractional value greater than .5 is not a midpoint value.
      if (fraction > .5)
         return (value + 1)/Math.Pow(10, digits);
      else
         return value/Math.Pow(10, digits);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:58,代码来源:Math.Round

输出:

Value       Full Precision        ToEven    AwayFromZero

11.1                 11.1            11              11
11.2                 11.2            11              11
11.3   11.299999999999999            11              11
11.4   11.399999999999999            11              11
11.5   11.499999999999998            12              12
11.6   11.599999999999998            12              12

11.5                 11.5            12              12

示例3: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      Single value = 16.325f;
      Console.WriteLine("Widening Conversion of {0:R} (type {1}) to {2:R} (type {3}): ", 
                        value, value.GetType().Name, (double) value, 
                        ((double) (value)).GetType().Name);
      Console.WriteLine(Math.Round(value, 2));
      Console.WriteLine(Math.Round(value, 2, MidpointRounding.AwayFromZero));
      Console.WriteLine();
      
      Decimal decValue = (decimal) value;
      Console.WriteLine("Cast of {0:R} (type {1}) to {2} (type {3}): ", 
                        value, value.GetType().Name, decValue, 
                        decValue.GetType().Name);
      Console.WriteLine(Math.Round(decValue, 2));
      Console.WriteLine(Math.Round(decValue, 2, MidpointRounding.AwayFromZero));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:23,代码来源:Math.Round

输出:

Widening Conversion of 16.325 (type Single) to 16.325000762939453 (type Double):
16.33
16.33

Cast of 16.325 (type Single) to 16.325 (type Decimal):
16.32
16.33

示例4:

decimal result = 0.0m;
decimal posValue =  3.45m;
decimal negValue = -3.45m;

// By default, round a positive and a negative value to the nearest even number. 
// The precision of the result is 1 decimal place.

result = Math.Round(posValue, 1);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, posValue);
result = Math.Round(negValue, 1);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, negValue);
Console.WriteLine();

// Round a positive value to the nearest even number, then to the nearest number away from zero. 
// The precision of the result is 1 decimal place.

result = Math.Round(posValue, 1, MidpointRounding.ToEven);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", result, posValue);
result = Math.Round(posValue, 1, MidpointRounding.AwayFromZero);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", result, posValue);
Console.WriteLine();

// Round a negative value to the nearest even number, then to the nearest number away from zero. 
// The precision of the result is 1 decimal place.

result = Math.Round(negValue, 1, MidpointRounding.ToEven);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", result, negValue);
result = Math.Round(negValue, 1, MidpointRounding.AwayFromZero);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", result, negValue);
Console.WriteLine();
开发者ID:.NET开发者,项目名称:System,代码行数:30,代码来源:Math.Round

输出:

3.4 = Math.Round( 3.45, 1)
-3.4 = Math.Round(-3.45, 1)

3.4 = Math.Round( 3.45, 1, MidpointRounding.ToEven)
3.5 = Math.Round( 3.45, 1, MidpointRounding.AwayFromZero)

-3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)
-3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)

示例5:

double posValue =  3.45;
 double negValue = -3.45;

 // Round a positive and a negative value using the default. 
 double result = Math.Round(posValue, 1);
 Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, posValue);
 result = Math.Round(negValue, 1);
 Console.WriteLine("{0,4} = Math.Round({1,5}, 1)\n", result, negValue);

 // Round a positive value using a MidpointRounding value. 
 result = Math.Round(posValue, 1, MidpointRounding.ToEven);
 Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", 
                   result, posValue);
 result = Math.Round(posValue, 1, MidpointRounding.AwayFromZero);
 Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)\n", 
                   result, posValue);

 // Round a negative value using a MidpointRounding value. 
 result = Math.Round(negValue, 1, MidpointRounding.ToEven);
 Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", 
                   result, negValue);
 result = Math.Round(negValue, 1, MidpointRounding.AwayFromZero);
 Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)\n", 
                   result, negValue);
开发者ID:.NET开发者,项目名称:System,代码行数:24,代码来源:Math.Round

输出:

3.4 = Math.Round( 3.45, 1)
-3.4 = Math.Round(-3.45, 1)

3.4 = Math.Round( 3.45, 1, MidpointRounding.ToEven)
3.5 = Math.Round( 3.45, 1, MidpointRounding.AwayFromZero)

-3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)
-3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)

示例6: foreach

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));
开发者ID:.NET开发者,项目名称:System,代码行数:4,代码来源:Math.Round

输出:

2.125 --> 2.13
2.135 --> 2.13
2.145 --> 2.15
3.125 --> 3.13
3.135 --> 3.14
3.145 --> 3.15

示例7: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      Double[] values = { 12.0, 12.1, 12.2, 12.3, 12.4, 12.5, 12.6, 
                          12.7, 12.8, 12.9, 13.0 };
      Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}", "Value", "Default", 
                        "ToEven", "AwayFromZero");
      foreach (var value in values)
         Console.WriteLine("{0,-10:R} {1,-10} {2,-10} {3,-15}",
                           value, Math.Round(value), 
                           Math.Round(value, MidpointRounding.ToEven),
                           Math.Round(value, MidpointRounding.AwayFromZero));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:18,代码来源:Math.Round

输出:

Value      Default    ToEven     AwayFromZero
12         12         12         12
12.1       12         12         12
12.2       12         12         12
12.3       12         12         12
12.4       12         12         12
12.5       12         12         13
12.6       13         13         13
12.7       13         13         13
12.8       13         13         13
12.9       13         13         13
13.0       13         13         13

示例8: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      double value = 11.1;
      for (int ctr = 0; ctr <= 5; ctr++)    
         value = RoundValueAndAdd(value);

      Console.WriteLine();

      value = 11.5;
      RoundValueAndAdd(value);
   }
   
   private static double RoundValueAndAdd(double value)
   {
      Console.WriteLine("{0} --> {1}", value, Math.Round(value, 
                        MidpointRounding.AwayFromZero));
      return value + .1;
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:24,代码来源:Math.Round

输出:

11.1 --> 11
11.2 --> 11
11.3 --> 11
11.4 --> 11
11.5 --> 11
11.6 --> 12

11.5 --> 12

示例9:

Math.Round(3.44, 1); //Returns 3.4.
Math.Round(3.45, 1); //Returns 3.4.
Math.Round(3.46, 1); //Returns 3.5.

Math.Round(4.34, 1); // Returns 4.3
Math.Round(4.35, 1); // Returns 4.4
Math.Round(4.36, 1); // Returns 4.4
开发者ID:.NET开发者,项目名称:System,代码行数:7,代码来源:Math.Round

示例10: Main

//引入命名空间
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));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:12,代码来源:Math.Round

输出:

2.125 --> 2.12
2.135 --> 2.13
2.145 --> 2.14
3.125 --> 3.12
3.135 --> 3.14
3.145 --> 3.14

示例11:

Console.WriteLine("Classic Math.Round in CSharp");
Console.WriteLine(Math.Round(4.4)); // 4
Console.WriteLine(Math.Round(4.5)); // 4
Console.WriteLine(Math.Round(4.6)); // 5
Console.WriteLine(Math.Round(5.5)); // 6
开发者ID:.NET开发者,项目名称:System,代码行数:5,代码来源:Math.Round

示例12: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      double value = 11.1;
      for (int ctr = 0; ctr <= 5; ctr++)    
         value = RoundValueAndAdd(value);

      Console.WriteLine();

      value = 11.5;
      RoundValueAndAdd(value);
   }
   
   private static double RoundValueAndAdd(double value)
   {
      Console.WriteLine("{0} --> {1}", value, Math.Round(value));
      return value + .1;
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:23,代码来源:Math.Round

输出:

11.1 --> 11
11.2 --> 11
11.3 --> 11
11.4 --> 11
11.5 --> 11
11.6 --> 12

11.5 --> 12

示例13:

Console.WriteLine(Math.Round(3.44m, 1)); 
Console.WriteLine(Math.Round(3.45m, 1)); 
Console.WriteLine(Math.Round(3.46m, 1)); 
Console.WriteLine();

Console.WriteLine(Math.Round(4.34m, 1)); 
Console.WriteLine(Math.Round(4.35m, 1)); 
Console.WriteLine(Math.Round(4.36m, 1));
开发者ID:.NET开发者,项目名称:System,代码行数:8,代码来源:Math.Round

输出:

3.4
3.4
3.5

4.3
4.4
4.4

示例14: for

Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}", "Value", "Default", 
                  "ToEven", "AwayFromZero");
for (decimal value = 12.0m; value <= 13.0m; value += 0.1m)
   Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}",
                     value, Math.Round(value), 
                     Math.Round(value, MidpointRounding.ToEven),
                     Math.Round(value, MidpointRounding.AwayFromZero));
开发者ID:.NET开发者,项目名称:System,代码行数:7,代码来源:Math.Round

输出:

Value      Default    ToEven     AwayFromZero
12         12         12         12
12.1       12         12         12
12.2       12         12         12
12.3       12         12         12
12.4       12         12         12
12.5       12         12         13
12.6       13         13         13
12.7       13         13         13
12.8       13         13         13
12.9       13         13         13
13.0       13         13         13

示例15: Main

//引入命名空间
using System;

public class MainClass {
    public static void Main() {

        Console.WriteLine(Math.Round(4.4));
        Console.WriteLine(Math.Round(4.5));
        Console.WriteLine(Math.Round(4.6));
        Console.WriteLine(Math.Round(5.5));
        Console.WriteLine(Math.Round(4.54, 1));
        Console.WriteLine(Math.Round(4.55, 1));
        Console.WriteLine(Math.Round(4.65, 1));
        Console.WriteLine(Math.Round(4.56, 1));
    }
}
开发者ID:C#程序员,项目名称:System,代码行数:16,代码来源:Math.Round


注:本文中的System.Math.Round方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。