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

## 備註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

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.