次の方法で共有


Decimal.Round メソッド

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

Public Shared Function Round( _
   ByVal d As Decimal, _   ByVal decimals As Integer _) As Decimal
[C#]
public static decimal Round(decimald,intdecimals);
[C++]
public: static Decimal Round(Decimald,intdecimals);
[JScript]
public static function Round(
   d : Decimal,decimals : int) : Decimal;

パラメータ

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

戻り値

小数点以下の桁数 decimals に丸められた d と等価の Decimal 数値。

例外

例外の種類 条件
ArgumentOutOfRangeException decimals が 0 から 28 までの範囲内の値ではありません。

解説

d がちょうど中間値であり、丸めた結果の数値が 2 つあり得る場合、小数部の右端が偶数になる方の値に丸められます。たとえば、小数点以下 2 桁に丸められる場合、値 2.345 は 2.34 になり、値 2.355 は 2.36 になります。この処理を偶数方向への丸めまたは近似値への丸めと呼びます。

使用例

[Visual Basic, C#, C++] Round メソッドを使用して Decimal の複数の値を指定された桁数に丸めるコード例を次に示します。

 
' Example of the Decimal.Round method. 
Imports System
Imports Microsoft.VisualBasic

Module DecimalRoundDemo
    
    Const dataFmt As String = "{0,26}{1,8}{2,26}"

    ' Display Decimal.Round parameters and the result.
    Sub ShowDecimalRound( Argument As Decimal, Digits As Integer )

        Dim rounded As Decimal = Decimal.Round( Argument, Digits )

        Console.WriteLine( dataFmt, Argument, Digits, rounded )
    End Sub

    Sub Main( )
        Console.WriteLine( "This example of the " & _
            "Decimal.Round( Decimal, Integer ) " & vbCrLf & _
            "method generates the following output." & vbCrLf )
        Console.WriteLine( dataFmt, "Argument", "Digits", "Result" )
        Console.WriteLine( dataFmt, "--------", "------", "------" )

        ' Create pairs of Decimal objects.
        ShowDecimalRound( 1.45D, 1 ) 
        ShowDecimalRound( 1.55D, 1 ) 
        ShowDecimalRound( 123.456789D, 4 ) 
        ShowDecimalRound( 123.456789D, 6 ) 
        ShowDecimalRound( 123.456789D, 8 ) 
        ShowDecimalRound( -123.456D, 0 ) 
        ShowDecimalRound( _
            New Decimal( 1230000000, 0, 0, True, 7 ), 3 )
        ShowDecimalRound( _
            New Decimal( 1230000000, 0, 0, True, 7 ), 11 )
        ShowDecimalRound( -9999999999.9999999999D, 9 )
        ShowDecimalRound( -9999999999.9999999999D, 10 )
    End Sub
End Module 

' This example of the Decimal.Round( Decimal, Integer )
' method generates 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

[C#] 
// Example of the decimal.Round method. 
using System;

class DecimalRoundDemo
{
    const string dataFmt = "{0,26}{1,8}{2,26}";

    // Display decimal.Round parameters and the result.
    public static void ShowDecimalRound( decimal Argument, int Digits )
    {
        decimal rounded = decimal.Round( Argument, Digits );

        Console.WriteLine( dataFmt, Argument, Digits, rounded );
    }

    public static void Main( )
    {
        Console.WriteLine( "This example of the " +
            "decimal.Round( decimal, Integer ) \n" +
            "method generates the following output.\n" );
        Console.WriteLine( dataFmt, "Argument", "Digits", "Result" );
        Console.WriteLine( dataFmt, "--------", "------", "------" );

        // Create pairs of decimal objects.
        ShowDecimalRound( 1.45M, 1 );
        ShowDecimalRound( 1.55M, 1 );
        ShowDecimalRound( 123.456789M, 4 );
        ShowDecimalRound( 123.456789M, 6 );
        ShowDecimalRound( 123.456789M, 8 );
        ShowDecimalRound( -123.456M, 0 );
        ShowDecimalRound( 
            new decimal( 1230000000, 0, 0, true, 7 ), 3 );
        ShowDecimalRound( 
            new decimal( 1230000000, 0, 0, true, 7 ), 11 );
        ShowDecimalRound( -9999999999.9999999999M, 9 );
        ShowDecimalRound( -9999999999.9999999999M, 10 );
    }
}

/*
This example of the decimal.Round( decimal, Integer )
method generates 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
*/

[C++] 
// Example of the Decimal::Round method. 
#using <mscorlib.dll>
using namespace System;

const __wchar_t* dataFmt = L"{0,26}{1,8}{2,26}" ;

// Display Decimal::Round parameters and the result.
void ShowDecimalRound( Decimal Argument, int Digits )
{
    Decimal rounded = Decimal::Round( Argument, Digits );

    Console::WriteLine( dataFmt, __box( Argument ), 
        __box( Digits ), __box( rounded ) );
}

void main( )
{
    Console::WriteLine( S"This example of the " 
        S"Decimal::Round( Decimal, Integer ) \n" 
        S"method generates the following output.\n" );
    Console::WriteLine( dataFmt, S"Argument", S"Digits", S"Result" );
    Console::WriteLine( dataFmt, S"--------", S"------", S"------" );

    // Create pairs of Decimal objects.
    ShowDecimalRound( Decimal::Parse( S"1.45" ), 1 );
    ShowDecimalRound( Decimal::Parse( S"1.55" ), 1 );
    ShowDecimalRound( Decimal::Parse( S"123.456789" ), 4 );
    ShowDecimalRound( Decimal::Parse( S"123.456789" ), 6 );
    ShowDecimalRound( Decimal::Parse( S"123.456789" ), 8 );
    ShowDecimalRound( Decimal::Parse( S"-123.456" ), 0 );
    ShowDecimalRound( Decimal( 1230000000, 0, 0, true, 7 ), 3 );
    ShowDecimalRound( Decimal( 1230000000, 0, 0, true, 7 ), 11 );
    ShowDecimalRound( 
        Decimal::Parse( S"-9999999999.9999999999" ), 9 );
    ShowDecimalRound( 
        Decimal::Parse( S"-9999999999.9999999999" ), 10 );
}

/*
This example of the Decimal::Round( Decimal, Integer )
method generates 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
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Decimal 構造体 | Decimal メンバ | System 名前空間 | Floor | Truncate