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
expressionis outside the range of the Decimal data type, an overflow error is generated.expressionmust 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 IfProgramming 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
expressionevaluates 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, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access