CInt - VBScript


Overview

The CInt command converts a string representation of an integer into a numeric data type. It is primarily used to parse numeric data from user input or other text sources. The result is a 32-bit signed integer value.

Syntax

CInt(string)
  • string: A string containing a numeric representation.

Options/Flags

None

Examples

Simple Usage:

Dim numString = "123"
Dim numValue = CInt(numString)
' numValue is now an integer with the value 123

Error Handling:

Dim numString = "abc"
On Error Resume Next
Dim numValue = CInt(numString)
If Err.Number <> 0 Then
  ' Handle the error (e.g., invalid numeric format)
End If

Combining Commands:

Dim userInput = InputBox("Enter a number:")
Dim numValue = CInt(Trim(userInput))
  • This example trims whitespace from the user input before converting it to an integer.

Common Issues

  • Invalid Input: If the input string does not represent a valid numeric value, CInt will return an error.
  • Overflow: If the numeric value is too large to fit in a 32-bit integer, the command will raise an overflow error.

Integration

CInt can be combined with other VB Script commands, such as InputBox to read user input or Trim to remove whitespace.

  • CBool: Converts a string to a Boolean value.
  • CDbl: Converts a string to a double-precision floating-point value.
  • CStr: Converts a value to a string.