Name
CSng Function
Syntax
CSng(expression)expressionUse: Required
Data Type: Numeric or String
The range of
expressionis -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values.
Return Value
expression cast as a type of
Single.
Description
Returns a single-precision number.
Rules at a Glance
expressionmust evaluate to a numeric value; otherwise, a type mismatch error is generated.If the value of
expressionis outside the range of the Single data type, an overflow error is generated.
Example
Dim sngMyNumber
If IsNumeric(sMyNumber) then
sngMyNumber = CSng(sMyNumber)
End IfProgramming Tips and Gotchas
If you need to use a floating-point number in VBScript, there is no reason to use a Single; use a Double instead. Generally, a Single is used because it offers better performance than a Double, but this is not true in VBScript. Not only is a Single not smaller than a Double in the VBScript implementation, but the processor also converts Singles to Doubles, performs any numeric operations, and then converts Doubles back to Singles.
Test that
expressionevaluates to a number by using the IsNumeric function.
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