Decimal 資料類型 (Visual Basic)Decimal Data Type (Visual Basic)

保存帶正負號的128位(16位元組)值,其代表以10的變數乘冪來調整的96位(12位元組)整數。Holds signed 128-bit (16-byte) values representing 96-bit (12-byte) integer numbers scaled by a variable power of 10. 縮放比例會指定小數點右邊的位數;其範圍從0到28。The scaling factor specifies the number of digits to the right of the decimal point; it ranges from 0 through 28. 若小數位數為0(不含小數點),則最大的可能值為 +/-79228162514264337593543950335 (+/-7.9228162514264337593543950335E + 28)。With a scale of 0 (no decimal places), the largest possible value is +/-79,228,162,514,264,337,593,543,950,335 (+/-7.9228162514264337593543950335E+28). 若有28位小數位數,最大值為 +/-7.9228162514264337593543950335,而最小非零值為 +/-0.0000000000000000000000000001 (+/-1E-28)。With 28 decimal places, the largest value is +/-7.9228162514264337593543950335, and the smallest nonzero value is +/-0.0000000000000000000000000001 (+/-1E-28).

備註Remarks

@No__t 0 資料類型提供數位的最大有效位數。The Decimal data type provides the greatest number of significant digits for a number. 最多可支援29個有效位數,而且可以代表超過 7.9228 x 10 ^ 28 的值。It supports up to 29 significant digits and can represent values in excess of 7.9228 x 10^28. 它特別適合需要大量數位但無法容忍進位誤差的計算(例如財務)。It is particularly suitable for calculations, such as financial, that require a large number of digits but cannot tolerate rounding errors.

Decimal 的預設值為 0。The default value of Decimal is 0.

程式設計提示Programming Tips

  • 精密.Precision. Decimal 不是浮點資料類型。Decimal is not a floating-point data type. @No__t-0 結構會保存一個二進位整數值,加上正負號位和整數的縮放因數,以指定值的哪個部分為小數部分。The Decimal structure holds a binary integer value, together with a sign bit and an integer scaling factor that specifies what portion of the value is a decimal fraction. 因此,@no__t 0 的數位在記憶體中的表示方式比浮點類型(SingleDouble)更精確。Because of this, Decimal numbers have a more precise representation in memory than floating-point types (Single and Double).

  • 效能。Performance. @No__t 0 資料類型是所有數數值型別的最慢。The Decimal data type is the slowest of all the numeric types. 在選擇資料類型之前,您應該先衡量精確度與效能的重要性。You should weigh the importance of precision against performance before choosing a data type.

  • 加寬.Widening. @No__t 0 資料類型會擴展為 SingleDoubleThe Decimal data type widens to Single or Double. 這表示您可以將 Decimal 轉換成其中一種類型,而不會遇到 System.OverflowException 錯誤。This means you can convert Decimal to either of these types without encountering a System.OverflowException error.

  • 尾端零。Trailing Zeros. Visual Basic 不會在 Decimal 常值中儲存尾端零。Visual Basic does not store trailing zeros in a Decimal literal. 不過,@no__t 0 變數會保留任何尾端零的計算。However, a Decimal variable preserves any trailing zeros acquired computationally. 下列範例將說明這點。The following example illustrates this.

    Dim d1, d2, d3, d4 As Decimal
    d1 = 2.375D
    d2 = 1.625D
    d3 = d1 + d2
    d4 = 4.000D
    MsgBox("d1 = " & CStr(d1) & ", d2 = " & CStr(d2) &
          ", d3 = " & CStr(d3) & ", d4 = " & CStr(d4))
    

    在上述範例中,MsgBox 的輸出如下所示:The output of MsgBox in the preceding example is as follows:

    d1 = 2.375, d2 = 1.625, d3 = 4.000, d4 = 4
    
  • 輸入字元。Type Characters. 將常值類型字元 D 附加到常值,會強制其成為 Decimal 資料類型。Appending the literal type character D to a literal forces it to the Decimal data type. 將識別項類型字元 @ 附加到任何識別項,會強制其成為 DecimalAppending the identifier type character @ to any identifier forces it to Decimal.

  • 架構類型。Framework Type. 在 .NET Framework 中對應的類型為 System.Decimal 結構。The corresponding type in the .NET Framework is the System.Decimal structure.

RangeRange

您可能需要使用 D 型別字符,將大數值指派給 @no__t 1 的變數或常數。You might need to use the D type character to assign a large value to a Decimal variable or constant. 這項需求是因為編譯器會將常值轉譯為 Long,除非常數值型別字元遵循常值,如下列範例所示。This requirement is because the compiler interprets a literal as Long unless a literal type character follows the literal, as the following example shows.

Dim bigDec1 As Decimal = 9223372036854775807   ' No overflow.
Dim bigDec2 As Decimal = 9223372036854775808   ' Overflow.
Dim bigDec3 As Decimal = 9223372036854775808D  ' No overflow.

@No__t-0 的宣告不會產生溢位,因為指派給它的值落在 Long 的範圍內。The declaration for bigDec1 doesn't produce an overflow because the value that's assigned to it falls within the range for Long. @No__t-0 值可指派給 @no__t 1 變數。The Long value can be assigned to the Decimal variable.

@No__t-0 的宣告會產生溢位錯誤,因為指派給它的值太大,而無法 LongThe declaration for bigDec2 generates an overflow error because the value that's assigned to it is too large for Long. 因為數值常值無法先解讀為 Long,所以無法將它指派給 @no__t 1 變數。Because the numeric literal can't first be interpreted as a Long, it can't be assigned to the Decimal variable.

對於 bigDec3,常數值型別字元 D 會強制編譯器將常值解讀為 Decimal,而不是 Long,藉此解決此問題。For bigDec3, the literal type character D solves the problem by forcing the compiler to interpret the literal as a Decimal instead of as a Long.

另請參閱See also