O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

CDec Function

Named Arguments

No

Syntax

CDec(expression)
expression (required; Numeric or String)

The range is +/-79,228,162,514,264,337,593,543,950,335 for numbers with no decimal places. The range is +/-7.9228162514264337593543950335 for numbers with up to 28 decimal places. The smallest possible nonzero number is 0.0000000000000000000000000001.

Return Value

expression cast as a Decimal type

Description

This function casts expression as a Decimal value.

Rules at a Glance

  • If the value of expression is outside the range of the Decimal data type, an overflow error is generated.

  • expression must evaluate to a numeric value; otherwise a type-mismatch error is generated. To prevent this, it can be tested beforehand with the IsNumeric function.

Example

Dim decMyNumber As Decimal
If IsNumeric(sMyNumber) then
    decMyNumber = CDec(sMyNumber)
End If

Programming Tips and Gotchas

  • The Decimal data type replaces the VB 6 Currency data type and is appropriate for very large, very small, or very high precision numbers.

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

  • When converting a string representation of a number to a numeric, you should use the data type conversion functions — such as CDec — instead of Val, because the data type conversion functions take account of the system’s regional settings. In particular, the CDec function recognizes the thousands separator if it is encountered in the string representation of a number. For example, if the user inputs the value 1,827,209.6654, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required