System.Decimal 結構

本文提供此 API 參考文件的補充備註。

Decimal實值型別代表十進制數,範圍從正 79,228,162,514,264,337,593,543,950,335 至負 79,228,162,514,264,337,593,543,950,335。 的預設值 Decimal 為 0。 實 Decimal 值類型適用於需要大量有效整數和小數位數且沒有捨入錯誤的財務計算。 此 Decimal 類型不會消除捨入的需求。 它會將因進位而產生的錯誤降到最低。 例如,下列程式代碼會產生 0.999999999999999999999999999999,而不是 1 的結果。

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))

十進位數是一個浮點值,其中包含符號、數值,其中值中的每個數位範圍從 0 到 9,以及一個縮放比例,表示分隔數值整數和小數部分的浮點位置。

值的二進位表示 Decimal 法是 128 位,其中包含 96 位整數,以及一組 32 位的旗標,代表用來指定小數分數部分的正負號和縮放比例等專案。 因此,格式 ((-2 96 到 2 96) / 10(0 到28)的二進位表示Decimal法,其中 -(296-1) 等於 MinValue,而 296-1 等於 。MaxValue 如需值的二進位表示 Decimal 法和範例的詳細資訊,請參閱建 Decimal(Int32[]) 構函式和 GetBits 方法。

縮放比例也會保留數位中的任何 Decimal 尾端零。 尾端零不會影響算術或比較運算中數位的值 Decimal 。 不過,如果套用適當的格式字串,方法可能會顯示 ToString 尾端零。

轉換考慮

此類型提供方法,可將值轉換成 Decimal 、、Int16、、Int32ByteInt64UInt16UInt32UInt64SByte值。 從這些整數型 Decimal 別轉換成 的轉換,會擴大永遠不會遺失資訊或擲回例外狀況的轉換。

Decimal 轉換為任何整數型別的轉換會將值四捨五入 Decimal 為最接近的整數值轉換為零。 某些語言,例如 C#,也支援將值Char轉換成Decimal值。 如果這些轉換的結果無法在目的地類型中表示, OverflowException 則會擲回例外狀況。

Decimal類型也提供方法,可將值與值Single轉換成 DecimalDouble 值。 從 Decimal 轉換到 SingleDouble 正在縮小轉換,可能會失去有效位數,但無法瞭解已轉換值的大小資訊。 轉換不會擲回例外狀況。

Single如果轉換的結果無法表示為 Decimal,則從 或 Double 轉換擲DecimalOverflowException例外狀況。

對十進位值執行作業

Decimal 類型支援標準數學運算,例如加法、減法、除法、乘法和一元負數。 您也可以呼叫 GetBits 方法,直接使用 值的二進位表示Decimal法。

若要比較兩個 Decimal 值,您可以使用標準數值比較運算符,也可以呼叫 CompareToEquals 方法。

您也可以呼叫 類別的成員 Math 來執行廣泛的數值運算,包括取得數字的絕對值、判斷兩 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