當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。