Siebel VB Language Reference > VB Language Reference >

CSng Function


This standard VB function converts an expression to the data type single.

Syntax

CSng(expression)

Argument
Description

expression

Any expression that can evaluate to a number

Returns

The value of expression as a single-precision floating-point number.

Usage

The expression must have a value within the range allowed for the single data type, or an error occurs.

Strings that cannot be converted to an integer result in a Type Mismatch error. Variants containing null result in an Illegal Use of Null error.

Example

This example calculates the factorial of a number. A factorial (notated with an exclamation mark, !) is the product of a number and each integer between it and the number 1. For example, 5 factorial, or 5!, is the product of 5*4*3*2*1, or the value 120.

Sub Button_Click
   Dim number as Integer
   Dim factorial as Double
   Dim msgtext As String
   number = 25
   If number <= 0 then
      Exit Sub
   End If

   factorial = 1
   For x = number to 2 step -1
      factorial = factorial * x
   Next x
      'If number  <= 35, then its factorial is small enough to
      ' be stored as a single-precision number
   If number< 35 then
      factorial = CSng(factorial)
   End If
   msgtext = "The factorial of " & number & " is " & factorial
End Sub

See Also

CCur Function
CDbl Function
CInt Function
CLng Function
CStr Function
CVar Function
CVDate Function

Siebel VB Language Reference