英語で読む

次の方法で共有


Decimal.Round メソッド

定義

最も近い整数または指定した小数点数に値を丸めます。

オーバーロード

Round(Decimal, Int32, MidpointRounding)

指定した丸め方法を使用して、10 進数の値を指定した精度に丸めます。

Round(Decimal, MidpointRounding)

指定した丸め方法を使用して、10 進数の値を整数に丸めます。

Round(Decimal)

10 進値を最も近い整数に丸めます。

Round(Decimal, Int32)

Decimal 値を、指定した小数点以下の桁数に丸めます。

Round(Decimal, Int32, MidpointRounding)

ソース:
Decimal.cs
ソース:
Decimal.cs
ソース:
Decimal.cs

指定した丸め方法を使用して、10 進数の値を指定した精度に丸めます。

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

パラメーター

d
Decimal

丸め対象の 10 進数。

decimals
Int32

戻り値の小数点以下の有効桁数 (精度)。

mode
MidpointRounding

使用する丸め方法を指定する列挙値の 1 つ。

戻り値

丸め方法を使用しmode、 の精度decimalsでに丸める数値dd の精度が decimals より小さい場合、d は変更されずに返されます。

実装

例外

decimals が 0 未満か、28 を超えています。

modeMidpointRounding 値ではありません。

結果は 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 の範囲を指定します。 が 0 の場合 decimals は、整数が返されます。

パラメーターに または AwayFromZeromode指定ToEvenした場合、これらの丸め方法は中間値、つまり最下位桁が 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

指定した丸め方法を使用して、10 進数の値を整数に丸めます。

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

パラメーター

d
Decimal

丸め対象の 10 進数。

mode
MidpointRounding

使用する丸め方法を指定する列挙値の 1 つ。

戻り値

丸め方法を d 使用して mode に丸められた整数。

実装

例外

modeMidpointRounding 値ではありません。

結果は 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

注釈

パラメーターに または AwayFromZeromode指定ToEvenした場合、これらの丸め方法は中間値、つまり最下位桁が 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

10 進値を最も近い整数に丸めます。

C#
public static decimal Round (decimal d);

パラメーター

d
Decimal

丸め対象の 10 進数。

戻り値

d パラメーターの値に最も近い整数。 d が 2 つの整数 (一方が偶数でもう一方が奇数) の中間にある場合は偶数が返されます。

実装

例外

結果は 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 に従います。 この種の丸めは、偶数または銀行家の丸呼ばれることもあります。 中間値を一方向に一貫して丸める結果となる丸めエラーを最小限に抑えます。 これは、 の引数MidpointRounding.ToEvenを使用して メソッドをmodeRound(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

丸め対象の 10 進数。

decimals
Int32

丸めた結果の数値の小数点以下の桁数を指定する 0 から 28 までの値。

戻り値

小数点以下の桁数に丸めた値に d 相当する decimals 10 進数。

実装

例外

decimals は、0 から 28 までの値ではありません。

次の例では、 メソッドを使用して、複数 Decimal の値を指定した小数点以下の桁数に Round 丸めます。

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

注釈

このメソッドは、 の引数MidpointRounding.ToEvenを指定して メソッドをRound(Decimal, Int32, MidpointRounding)mode呼び出すことと同じです。 が 2 つの丸められた値のちょうど中間にある場合 d 、結果は、右端の小数点以下に偶数の数字を持つ丸められた値になります。 たとえば、小数点以下 2 桁に丸められる場合、値 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