CCur - VBScript
Overview
CCur converts a string expression to a currency value. It is particularly useful for converting string representations of monetary values into actual currency values for calculations or data processing.
Syntax
Number = CCur(StringExpression)
- StringExpression: Required. The string expression to be converted to a currency value. It can be any valid string that represents a monetary value, such as “123.45”, “$123.45”, or “123,456.78”.
Options/Flags
None.
Examples
Example 1: Simple Conversion
myValue = CCur("123.45") ' Converts "123.45" to currency value
Example 2: Removing Non-Numeric Characters
myValue = CCur("($123.45)") ' Removes non-numeric characters, converts to currency
Example 3: Handling Commas as Decimal Separators
myValue = CCur("123,456.78") ' Converts "123,456.78" with commas as decimal separators
Common Issues
- Invalid Input: If the StringExpression is not a valid monetary value (e.g., contains non-numeric characters), an error will be raised.
- Decimal Place Precision: CCur may truncate decimal places depending on the system settings. Use FormatNumber function for greater precision.
- Negative Values: Enclose negative values in parentheses for correct conversion (e.g., CCur(“($123.45)”)).
Integration
CCur can be used in conjunction with other VB Script commands and functions, such as:
- FormatNumber: Format the currency value to a specific format.
- Str: Convert the currency value back to a string.
Related Commands
- CBool: Converts a string expression to a Boolean value.
- CByte: Converts a string expression to a byte value.
- CDate: Converts a string expression to a date value.