System.Decimal 構造体

この記事では、この API のリファレンス ドキュメントへの補足的な解説を提供します。

値型は Decimal 、正の 79,228,162,514,264,337,593,543,950,100 の範囲の 10 進数を表します。 335 からマイナス 79,228,162,514,264,337,593,543,950,335。 既定値の a Decimal は 0 です。 この値の型は Decimal 、整数と小数部の数字を大量に必要とし、丸め誤差がない財務計算に適しています。 この型では Decimal 、丸める必要はありません。 丸めに起因するエラーが最小限に抑えられます。 たとえば、次のコードは、1 ではなく 0.9999999999999999999999999999 という結果を生成します。

decimal dividend = Decimal.One;
decimal divisor = 3;
// The following displays 0.9999999999999999999999999999 to the console
Console.WriteLine(dividend/divisor * divisor);
let dividend = Decimal.One
let divisor = 3m
// The following displays 0.9999999999999999999999999999 to the console
printfn $"{dividend/divisor * divisor}"
Dim dividend As Decimal = Decimal.One
Dim divisor As Decimal = 3
' The following displays 0.9999999999999999999999999999 to the console
Console.WriteLine(dividend/divisor * divisor)

除算と乗算の結果がメソッドに Round 渡されると、次のコードに示すように、結果は精度を失いません。

decimal dividend = Decimal.One;
decimal divisor = 3;
// The following displays 1.00 to the console
Console.WriteLine(Math.Round(dividend/divisor * divisor, 2));
let dividend = Decimal.One
let divisor = 3m
// The following displays 1.00 to the console
printfn $"{Math.Round(dividend/divisor * divisor, 2)}"
Dim dividend As Decimal = Decimal.One
Dim divisor As Decimal = 3
' The following displays 1.00 to the console
Console.WriteLine(Math.Round(dividend/divisor * divisor, 2))

10 進数は、符号、値の各桁の範囲が 0 から 9 の数値、および数値の整数部分と小数部を区切る浮動小数点の位置を示す拡大縮小率で構成される浮動小数点値です。

値の Decimal バイナリ表現は、96 ビットの整数で構成される 128 ビットと、10 進数の部分を指定するために使用される符号や拡大縮小率などを表す 32 ビットフラグのセットです。 したがって、(-2 96 から 296) / 10(0 から 28))、-(2 96-1) が等しく、296-1 が等しいMinValueMaxValue値のバイナリ表現Decimal。 値のDecimalバイナリ表現と例の詳細については、コンストラクターとメソッドをDecimal(Int32[])GetBits参照してください。

スケール ファクターでは、末尾のゼロも数値に Decimal 保持されます。 末尾のゼロは、算術演算または比較演算の数値には影響 Decimal しません。 ただし、適切な書式指定文字列が適用されている場合は、メソッドによって ToString 末尾のゼロが表示される可能性があります。

変換に関する考慮事項

この型は、値を 、、Int16UInt16Int64ByteInt32UInt32および値とのSByte間で変換DecimalするメソッドをUInt64提供します。 これらの整数型から変換は Decimal 、情報を失ったり例外をスローしたりしない拡大変換です。

Decimal整数型から任意の整数型への変換では、値を最も近い整数値に 0 に丸Decimalめる縮小変換が行われます。 C# などの一部の言語では、値から Decimal 値への Char 変換もサポートされています。 変換先の型でこれらの変換の結果を表すことができない場合は、 OverflowException 例外がスローされます。

この型にはDecimal、値の変換元とSingleDouble値を変換Decimalするメソッドも用意されています。 変換の変換元DecimalSingleまたはDouble縮小変換では、精度が失われる可能性がありますが、変換された値の大きさに関する情報は失われる可能性があります。 変換によって例外がスローされることはありません。

変換の結果を > としてDecimal表すことができない場合に、例外をスローする変換またはSingleDouble例外をスローOverflowExceptionする変換。Decimal

Decimal 値に対して操作を実行する

この型は Decimal 、加算、減算、除算、乗算、単項否定などの標準的な数学演算をサポートします。 メソッドを呼び出GetBitsすことで、値のバイナリ表現をDecimal直接操作することもできます。

2 つのDecimal値を比較するには、標準の数値比較演算子を使用するか、またはEqualsメソッドをCompareTo呼び出すことができます。

クラスのメンバーを呼び出して、数値の Math 絶対値の取得、2 つの Decimal 値の最大値または最小値の決定、数値の符号の取得、数値の丸めなど、さまざまな数値演算を実行することもできます。

次のコード例は、Decimal の使用方法を示します。

/// <summary>
/// Keeping my fortune in Decimals to avoid the round-off errors.
/// </summary>
class PiggyBank {
    protected decimal MyFortune;

    public void AddPenny() {
        MyFortune = Decimal.Add(MyFortune, .01m);
    }

    public decimal Capacity {
        get {
            return Decimal.MaxValue;
        }
    }

    public decimal Dollars {
        get {
            return Decimal.Floor(MyFortune);
        }
    }

    public decimal Cents {
        get {
            return Decimal.Subtract(MyFortune, Decimal.Floor(MyFortune));
        }
    }

    public override string ToString() {
        return MyFortune.ToString("C")+" in piggy bank";
    }
}
/// Keeping my fortune in Decimals to avoid the round-off errors.
type PiggyBank() =
    let mutable myFortune = 0m

    member _.AddPenny() =
        myFortune <- Decimal.Add(myFortune, 0.01m)

    member _.Capacity =
        Decimal.MaxValue

    member _.Dollars =
        Decimal.Floor myFortune

    member _.Cents =
        Decimal.Subtract(myFortune, Decimal.Floor myFortune)

    override _.ToString() =
        $"{myFortune:C} in piggy bank"
' Keeping my fortune in Decimals to avoid the round-off errors.
Class PiggyBank
    Protected MyFortune As Decimal

    Public Sub AddPenny()
        MyFortune = [Decimal].Add(MyFortune, 0.01D)
    End Sub

    Public ReadOnly Property Capacity() As Decimal
        Get
            Return [Decimal].MaxValue
        End Get
    End Property

    Public ReadOnly Property Dollars() As Decimal
        Get
            Return [Decimal].Floor(MyFortune)
        End Get
    End Property

    Public ReadOnly Property Cents() As Decimal
        Get
            Return [Decimal].Subtract(MyFortune, [Decimal].Floor(MyFortune))
        End Get
    End Property

    Public Overrides Function ToString() As String
        Return MyFortune.ToString("C") + " in piggy bank"
    End Function
End Class