How to: Hold Money Values in a Variable

A variable holds money values if you declare it to be of data type Decimal.

A variable of the Decimal Data Type (Visual Basic) can hold numbers with 29 significant digits, including as many as 28 decimal places. For money values, you normally need only 2 or 3 decimal places. But for intermediate results during a transaction, such as multiplying by an interest rate, you might want additional decimal places to retain accuracy.

The advantage of using Decimal variables for money values is the precision of the values. The Double data type is faster and requires less memory, but it is subject to rounding errors. The Decimal data type retains complete accuracy to 28 places if necessary.

Trailing 0 Characters. Visual Basic does not have any internal representation of trailing 0 characters in the floating-point data types such as Double. It does not store trailing zeros in a Decimal literal, although a Decimal variable preserves any trailing zeros acquired computationally. For more information and an example, see Decimal Data Type (Visual Basic).

Visual Basic often suppresses trailing 0 characters when displaying or printing. For example, a value of 4.2000 is often rendered as 4.2. You can control the output format more closely if you call the Decimal.ToString or Double.ToString method for the appropriate data type.

To hold money values in a variable

  1. Declare the variable with a Dim Statement (Visual Basic).

  2. Follow the variable name with an As clause.

  3. Follow the As keyword with the Decimal keyword.

See Also

Concepts

Data Types in Visual Basic

Type Characters

Reference

Data Type Summary (Visual Basic)

Single Data Type (Visual Basic)

Decimal Data Type (Visual Basic)

Double Data Type (Visual Basic)

Other Resources

Elementary Data Types