Data Type Conversion Functions
See Also Example
Explicitly converts expressions from one data type
Syntax
CCur(expression)
CDbl(expression)
CInt(expression)
CLng(expression)
CSng(expression)
CStr(expression)
CVar(expression)
Remarks
The argument expression can be any valid string expression
The following table shows which data type is returned by each data type conversion function.
Function From To
CCur Any valid expression Currency
CDbl Any valid expression Double
CInt Any valid expression Integer
CLng Any valid expression Long
CSng Any valid expression Single
CStr Any valid expression String
CVar Any valid expression Variant
The numeric conversion functions CCur, CDbl, CInt, CLng, and CSng explicitly control the data type of a numeric expression. For example, you can use CCur to force currency arithmetic (which has greater precision but less range) in cases in which integer, double-precision, or single-precision arithmetic normally would occur. CInt and CLng force integer arithmetic in cases in which currency, single-precision, or double-precision arithmetic normally would occur. CDbl and CSng force double-precision or single-precision arithmetic in cases in which currency or integer arithmetic normally would occur.
Tip Use these data type conversion functions to document your code to show that the result of a calculation should be expressed as a particular data type rather than the normal data type of the result.
For the CCur, CInt, CLng, and CSng functions, if expression lies outside the acceptable range, an Overflow error occurs and a message is displayed unless it is trapped in error-handling code.
Note CInt differs from the Fix and Int functions, which truncate, rather than round, the fractional part of a number. When the fractional part is exactly 0.5, the CInt function always rounds it to the nearest even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2.
Visual Basic Data Types
Int Function, Fix Function
Data Type Conversion Function Examples
CCur Function Example
CDbl Function Example
CInt Function Example
CLng Function Example
CSng Function Example
CStr Function Example
CVar Function Example
CCur converts a value calculated as a Double to a Currency with four decimal digits of precision to the right of the decimal place, displaying the result in conventional currency value format with two places to the right of the decimal. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
Dim Amount, Msg, Percent, SalesTax ' Declare variables.
Percent = 8.2 ' Set tax percentage rate.
Msg = "What is the taxable currency amount?"
Amount = InputBox(Msg) ' Get user input.
SalesTax = CCur(Amount * (Percent * .01)) ' Calculate tax.
Msg = "Tax on " & Format(Amount, "Currency")
Msg = Msg & " is " & Format(SalesTax, "Currency")
MsgBox Msg ' Display results.
End Sub
The CDbl function converts a Currency value to a Double. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
Dim CurrVal, CurrPercent, Msg, Percent ' Declare variables.
Percent = 8.2 ' Set tax rate.
Msg = "Enter a currency value."
CurrVal = CCur(InputBox(Msg)) ' Get user input.
CurrPercent = CDbl(CurrVal * Percent * .01) ' Calculate value.
Msg = Percent & "% of "
Msg = Msg & Format(CurrVal, "Currency")
Msg = Msg & " is $" & CurrPercent
Msg = Msg & " expressed as a double-precision number."
MsgBox Msg ' Display results.
End Sub
The example converts an angle in radians to an angle in degrees and minutes. The CInt function is used to convert a double-precision value to an integer value. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
Dim Degs, Mins, Pi, Msg ' Declare variables.
Dim Rads, Rads2Degs, WholeDegs, WholeMins
Pi = 4 * Atn(1) ' Calculate Pi.
Rads2Degs = 180 / Pi ' Calculate conversion factor.
Msg = "Please enter an angle in radians."
Rads = InputBox(Msg) ' Get angle in radians.
Degs = Rads * Rads2Degs ' Convert radians to degrees.
Mins = Degs - Int(Degs) ' Get fractional part.
WholeMins = CInt(Mins * 60) ' Convert to between 0 and 60.
WholeDegs = Int(Degs) ' Get whole number part.
If WholeMins = 60 Then ' 60 minutes = 1 degree.
WholeDegs = WholeDegs + 1
WholeMins = 0
End If
Msg = "Angle = " & WholeDegs & " degrees, "
Msg = Msg & WholeMins & " minutes."
MsgBox Msg ' Display results.
End Sub
The example uses the CLng function to convert double-precision numbers to long integers. It also demonstrates how CLng rounds numbers when converting. Note that 25427.45 is rounded down to 25427, whereas 25427.55 is rounded up to 25428. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
NL = Chr(13) & Chr(10) ' Define newline.
Msg = "25427.45 rounds to " & CLng(25427.45) & "."
Msg = Msg & NL & "25427.55 rounds to "
Msg = Msg & CLng(25427.55) & "."
MsgBox Msg ' Display results.
End Sub
The example uses the CSng function to convert double-precision numbers to single-precision. It also demonstrates how CSng rounds numbers when converting. Note that 75.3421115 is rounded down to 75.34211, whereas 75.3421155 is rounded up to 75.34212. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
Dim Msg, NL ' Declare variables.
NL = Chr(13) & Chr(10) ' Define newline.
Msg = "75.3421115 rounds to " & CSng(75.3421115)
Msg = Msg & "." & NL & "75.3421555 rounds to "
Msg = Msg & CSng(75.3421555) & "."
MsgBox Msg ' Display results.
End Sub
The example uses the CStr function to convert a Double to a String. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
Dim Msg, NumVal, StrVal ' Declare variables.
Msg = "Enter some numeric value."
NumVal = InputBox(Msg) ' Get user input.
StrVal = CStr(NumVal) ' Perform conversion.
Msg = "StrVal is a Variant containing a number that has "
Msg = Msg & "been converted to a String."
MsgBox Msg ' Display message.
End Sub
CVar takes a String value expressed in thousands, concatenates three zeros, and converts the entire expression to a Variant. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
Dim Msg, SalesInK, TotalUnits ' Declare variables.
SalesInK = InputBox("How many thousand units did you sell?")
TotalUnits = CVar(SalesInK & "000")
Msg = "You sold " & Format(TotalUnits, "###,###") & " units."
MsgBox Msg ' Display message.
End Sub