閱讀英文

共用方式為


Decimal.Round 方法

定義

將值捨入至最接近的整數或是指定的小數位數數字。

多載

Round(Decimal, Int32, MidpointRounding)

使用指定的進位策略,將十進位值四捨五入至指定的有效位數。

Round(Decimal, MidpointRounding)

使用指定的進位策略,將十進位值四捨五入為整數。

Round(Decimal)

將十進位值四捨五入為最接近的整數。

Round(Decimal, Int32)

Decimal 值捨入為指定的小數位數。

Round(Decimal, Int32, MidpointRounding)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

使用指定的進位策略,將十進位值四捨五入至指定的有效位數。

C#
public static decimal Round (decimal d, int decimals, MidpointRounding mode);

參數

d
Decimal

要捨入的十進位數字。

decimals
Int32

在傳回值中的有效小數位數 (精確度)。

mode
MidpointRounding

其中一個列舉值,指定要使用哪一個進位策略。

傳回

使用mode捨入策略和精確度decimals為的四捨五入的數位d。 如果 d 的精確度少於 decimals,則 d 傳回時不會變更。

實作

例外狀況

decimals 小於 0 或大於 28。

mode 不是 MidpointRounding 值。

結果超出 Decimal 物件的範圍。

範例

下列範例示範如何使用 Round(Decimal, Int32, MidpointRounding) 方法搭配 MidpointRounding 列舉。

C#
decimal result;

// Round a positive value using different strategies.
// The precision of the result is 1 decimal place.

result = Math.Round(3.45m, 1, MidpointRounding.ToEven);
Console.WriteLine($"{result} = Math.Round({3.45m}, 1, MidpointRounding.ToEven)");
result = Math.Round(3.45m, 1, MidpointRounding.AwayFromZero);
Console.WriteLine($"{result} = Math.Round({3.45m}, 1, MidpointRounding.AwayFromZero)");
result = Math.Round(3.47m, 1, MidpointRounding.ToZero);
Console.WriteLine($"{result} = Math.Round({3.47m}, 1, MidpointRounding.ToZero)\n");

// Round a negative value using different strategies.
// The precision of the result is 1 decimal place.

result = Math.Round(-3.45m, 1, MidpointRounding.ToEven);
Console.WriteLine($"{result} = Math.Round({-3.45m}, 1, MidpointRounding.ToEven)");
result = Math.Round(-3.45m, 1, MidpointRounding.AwayFromZero);
Console.WriteLine($"{result} = Math.Round({-3.45m}, 1, MidpointRounding.AwayFromZero)");
result = Math.Round(-3.47m, 1, MidpointRounding.ToZero);
Console.WriteLine($"{result} = Math.Round({-3.47m}, 1, MidpointRounding.ToZero)\n");

/*
This code example produces the following results:

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

-3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)
-3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)
-3.4 = Math.Round(-3.47, 1, MidpointRounding.ToZero)
*/

備註

參數 decimals 會指定傳回值中的有效小數字數數目,範圍從 0 到 28。 如果 decimals 為零,則會傳回整數。

如果您指定 ToEvenAwayFromZero 參數 mode ,這些捨入策略只會套用至中間點值,也就是最小有效位數為 5 的值。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Round(Decimal, MidpointRounding)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

使用指定的進位策略,將十進位值四捨五入為整數。

C#
public static decimal Round (decimal d, MidpointRounding mode);

參數

d
Decimal

要捨入的十進位數字。

mode
MidpointRounding

其中一個列舉值,指定要使用哪一個進位策略。

傳回

使用捨入策略四捨五入的mode整數d

實作

例外狀況

mode 不是 MidpointRounding 值。

結果超出 Decimal 物件的範圍。

範例

下列範例會顯示方法傳 Round(Decimal, MidpointRounding) 回的值,其中包含不同的 mode 自變數。

C#
Console.WriteLine($"{"Value",-10} {"Default",-10} {"ToEven",-10} {"AwayFromZero",-15} {"ToZero",-15}");
for (decimal value = 12.0m; value <= 13.0m; value += 0.1m)
    Console.WriteLine($"{value,-10} {Math.Round(value),-10} " +
        $"{Math.Round(value, MidpointRounding.ToEven),-10} " +
        $"{Math.Round(value, MidpointRounding.AwayFromZero),-15} " +
        $"{Math.Round(value, MidpointRounding.ToZero),-15}");

// The example displays the following output:
//       Value      Default    ToEven     AwayFromZero    ToZero
//       12.0       12         12         12              12
//       12.1       12         12         12              12
//       12.2       12         12         12              12
//       12.3       12         12         12              12
//       12.4       12         12         12              12
//       12.5       12         12         13              12
//       12.6       13         13         13              12
//       12.7       13         13         13              12
//       12.8       13         13         13              12
//       12.9       13         13         13              12
//       13.0       13         13         13              13

備註

如果您指定 ToEvenAwayFromZero 參數 mode ,這些捨入策略只會套用至中間點值,也就是最小有效位數為 5 的值。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Round(Decimal)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

將十進位值四捨五入為最接近的整數。

C#
public static decimal Round (decimal d);

參數

d
Decimal

要捨入的十進位數字。

傳回

最接近 d 參數的整數。 如果 d 介於兩個整數中間,一個為偶數,一個為奇數,則會傳回偶數。

實作

例外狀況

結果超出 Decimal 值的範圍。

範例

下列範例會將介於100到102之間的值範圍 Decimal 四捨五入至最接近的整數。 因為方法使用銀行者的捨入,所以 100.5 捨入至 100,而 101.5 捨入為 102。

C#
using System;

public class Example
{
   public static void Main()
   {
      for (decimal value = 100m; value <= 102m; value += .1m)
         Console.WriteLine("{0} --> {1}", value, Decimal.Round(value));
   }
}
// The example displays the following output:
//     100 --> 100
//     100.1 --> 100
//     100.2 --> 100
//     100.3 --> 100
//     100.4 --> 100
//     100.5 --> 100
//     100.6 --> 101
//     100.7 --> 101
//     100.8 --> 101
//     100.9 --> 101
//     101.0 --> 101
//     101.1 --> 101
//     101.2 --> 101
//     101.3 --> 101
//     101.4 --> 101
//     101.5 --> 102
//     101.6 --> 102
//     101.7 --> 102
//     101.8 --> 102
//     101.9 --> 102
//     102.0 --> 102

備註

此方法的行為遵循 IEEE Standard 754 第 4 節。 這種捨入有時稱為四捨五入 到偶 數或 銀行者的捨入。 它會將單一方向中點值持續四捨五入所產生的進位錯誤降到最低。 它相當於使用 modeMidpointRounding.ToEven自變數呼叫 Round(Decimal, MidpointRounding) 方法。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Round(Decimal, Int32)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

Decimal 值捨入為指定的小數位數。

C#
public static decimal Round (decimal d, int decimals);

參數

d
Decimal

要捨入的十進位數字。

decimals
Int32

從 0 到 28 的值,這個值指定要捨入到的小數位數。

傳回

等於四捨五 d 入至 decimals 小數位數的小數字數。

實作

例外狀況

decimals 不是從 0 到 28 的值。

範例

下列範例會使用 Round 方法,將數個值四捨五Decimal入至指定的小數字數。

C#
using System;

class Example12
{
   public static void Main()
   {
      // Define a set of Decimal values.
      decimal[] values = { 1.45m, 1.55m, 123.456789m, 123.456789m,
                           123.456789m, -123.456m,
                           new Decimal(1230000000, 0, 0, true, 7 ),
                           new Decimal(1230000000, 0, 0, true, 7 ),
                           -9999999999.9999999999m,
                           -9999999999.9999999999m };
      // Define a set of integers to for decimals argument.
      int[] decimals = { 1, 1, 4, 6, 8, 0, 3, 11, 9, 10};

      Console.WriteLine("{0,26}{1,8}{2,26}",
                        "Argument", "Digits", "Result" );
      Console.WriteLine("{0,26}{1,8}{2,26}",
                        "--------", "------", "------" );
      for (int ctr = 0; ctr < values.Length; ctr++)
        Console.WriteLine("{0,26}{1,8}{2,26}",
                          values[ctr], decimals[ctr],
                          Decimal.Round(values[ctr], decimals[ctr]));
    }
}
// The example displays the following output:
//                   Argument  Digits                    Result
//                   --------  ------                    ------
//                       1.45       1                       1.4
//                       1.55       1                       1.6
//                 123.456789       4                  123.4568
//                 123.456789       6                123.456789
//                 123.456789       8                123.456789
//                   -123.456       0                      -123
//               -123.0000000       3                  -123.000
//               -123.0000000      11              -123.0000000
//     -9999999999.9999999999       9    -10000000000.000000000
//     -9999999999.9999999999      10    -9999999999.9999999999

備註

這個方法相當於使用 modeMidpointRounding.ToEven自變數呼叫 Round(Decimal, Int32, MidpointRounding) 方法。 當 d 介於兩個四捨五入值之間的一半時,結果會是四捨五入的值,其位在最右邊的小數點位置中有偶數位數。 例如,在四捨五入為兩個小數位數時,值 2.345 會變成 2.34,而值 2.355 會變成 2.36。 此程式稱為捨 入偶數,或 銀行員捨入。 它會將單一方向中點值持續四捨五入所產生的進位錯誤降到最低。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1