CDec Function

   
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, CDec converts it to the decimal value 1827209.6654, while Val converts it to a Double value of 1.

  • Like most of the conversion functions, CDec is not actually a function in the Microsoft.VisualBasic namespace. Instead, it is similar to a Visual C++ macro; the compiler translates the function call into inline code.

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net