Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))

CDec Function

Syntax

Dim result As Decimal = CDec(expression)

expression (required; Numeric or String)

Any expression in the valid range of the Decimal data type

Description

The CDec function converts expression to the Decimal data type.

Usage at a Glance

  • An expression that evaluates outside the valid range of the target data type results in a runtime error. Nonnumeric expressions also generate an error.

  • In most cases, the numeric conversion functions are a better choice than the Val function when converting a string, as the conversion functions take into account the system's regional settings. However, Val converts empty strings to zero without error.

  • Use IsNumeric to test whether expression evaluates to a number.

  • This function does not support named arguments.

Example

Dim targetNumber As Decimal If IsNumeric(stringNumber) Then targetNumber = CDec(stringNumber) End If

Version Differences

The Decimal data type replaces the VB 6 Currency data type, and it is appropriate for high-precision numbers.

See Also

Val Function

Категории