CDbl Function |
Named Arguments
No
Syntax
CDbl(expression)
expression
Use: Required
Data Type: Numeric or String
–1.79769313486232E308 to –4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values.
Return Value
expression cast as a Double data type.
Description
Converts expression to a Double data type.
Rules at a Glance
If the value of expression is outside the range of the double data type, an overflow error is generated.
Expression must evaluate to a numeric value; otherwise, a type mismatch error is generated.
Example
Dim dblMyNumber as Double If IsNumeric(sMyNumber) then dblMyNumber = CDbl(sMyNumber) End If
Programming Tips and Gotchas
When converting a string representation of a number to a numeric, you should use the data type conversion functions—such as CDbl—instead of Val, because the data type conversion functions take account of the system's regional settings. While CDbl recognizes and handles the thousands separator, the Val function can't. If a user inputs a value of 6,231,532.11, for example, CDbl correctly converts it to a double with a value of 6231532.11, while Val returns a value of 6.
Use IsNumeric to test whether expression evaluates to a number.
See Also
- FormatNumberFunction, IsNumeric Function, Val Function
Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.