### Convert String to Number

To convert a string to a Number, assign the String to the Numbertype wanted.
Example
Log("Convert String Test")
Dim s As String = "1234.56"
Log("String = " & s)
Dim i As Int = s
Log("Int = " & i)
Dim f As Float = s
Log("Float = " & f)
Dim d As Double = s
Log("Double = " & d)
Dim h As Short = s
Log("Short = " & h)
Result
Convert String Test
String = 1234.56
Int = 1234
Float = 1234.56005859375
Double = 1234.56
Short = 1234
Example with Exception handling
Log("Convert String Test")
Dim s As String = "A1234.56"
Try
Log("String = " & s)
Dim i As Int = s
Log("Int = " & i)
Dim f As Float = s
Log("Float = " & f)
Dim d As Double = s
Log("Double = " & d)
Dim h As Short = s
Log("Short = " & h)
Catch
Log(LastException.Message)
End Try
Result
Convert String Test
String = A1234.56
java.lang.NumberFormatException: For input string: "A1234.56"
Example using IsNumber check
Dim s As String = "A1234.56"
If IsNumber(s) = False Then
Log(s & " is not a number")
Return
End If
Result
Convert String Test
A1234.56 is not a number
Example with Formatting
Log("Convert String Test")
Dim s As String = "1234.56123456789"
If IsNumber(s) = False Then
Log(s & " is not a number")
Return
End If
Try
Log("String = " & s)
Dim i As Int = s
Log("Int = " & i)
Dim f As Float = s
Log("Float = " & f)
Dim d As Double = s
Log("Double = " & d)
Dim h As Short = s
Log("Short = " & h)
Log("Rounding")
Dim f As Float = s
Log("Float Rounded (2 digits): " & NumberFormat(f,1,2))
Log("Float Cut: (2 digits)" & (Floor(f*100) / 100))
Catch
Log(LastException.Message)
End Try
Result
Convert String Test
String = 1234.56123456789
Int = 1234
Float = 1234.561279296875
Double = 1234.56123456789
Short = 1234
Rounding
Float Rounded: 1,234.56
Float Cut: 1234.56

Changed: , Category: Strings, File: T6C255M1844.htm, ID: 1844