CSng
Coerces an expression to a Single.
Syntax: CSng( expression )
- expression
- required Any valid string or numeric expression in the Single range —
-3.402823E38to-1.401298E-45for negative values, and1.401298E-45to3.402823E38for positive values.
The return type is Single.
CSng is the internationally aware alternative to Val for converting a string to a numeric type.
Common Errors
| Error | When |
|---|---|
6 (&H6), Overflow | expression is less than -3.402823E38 or greater than 3.402823E38 |
13 (&HD), Type mismatch | expression is a string that does not hold a number (a zero-length string included) |
94 (&H5E), Invalid use of Null | expression is Null |
Example
This example uses the CSng function to convert values to a Single.
Dim MyDouble1, MyDouble2, MySingle1, MySingle2
' MyDouble1, MyDouble2 are Doubles.
MyDouble1 = 75.3421115: MyDouble2 = 75.3421555
MySingle1 = CSng(MyDouble1) ' MySingle1 contains 75.34211.
MySingle2 = CSng(MyDouble2) ' MySingle2 contains 75.34216.