Conversion & Validation Functions

advertisement
Conversion, Validation and Format Functions
Cint(Str): Converts a number to an integer math value. If the number is double it is rounded.
Example:
Dim NumberOne As Integer
NumberOne = CInt(txtFirstNum.Text)
CDbl(Str): Converts a number to a double math value.
Example:
Dim NumberOne As Double
NumberTwo = CDbl(txtFirstNum.Text)
CStr(Number): Converts a number to string. Typically used to convert a value to string before
assigning to any output as text.
Example:
Dim NumberOne As Double
txtFirstNum.text = CStr(NumberOne)
Alternatives to Cint, CDbl, and CStr:
Let intVar, dblVar, and strVar are respectively defined as integer, double and string data types.
strVar = CType(dblVar, String)
strVar = Convert.ToString(dblVar)
strVar = dblVar.ToString
intVar = CType(strVar, Integer)
intVar = CType(dblVar, Integer)
intVar = Integer.Parse(strVar)
dblVar = CType(strVar, Double)
dblVar = CType(intVar, Double)
dblVar = Double.Parse(strVar)
intVar = Convert.ToInt32(strVar)
intVar = Convert.ToInt32(dblVar)
dblVar= Convert.ToDouble(strVar)
dblVar= Convert.ToDouble(intVar)
IsNumeric(strVar): Used for Validation
The expression IsNumeric(strVar) is true if the value of strVar can be converted to a number
with CInt or CDbl.
Examples:
IsNumeric("123") is true
IsNumeric("$123") is true
IsNumeric("3 - 2") is false
If (IsNumeric(txtBox.Text) = True) Then
or
If IsNumeric(txtBox.Text) Then
FormatNumber(Number):
FormatNumber(12345.678,2))
‘up to two decimal places
txtOutput.text = CStr(FormatNumber(12345.678,1))
‘up to one decimal place
FormatCurrency(Number): Formats a number to currency format ($ with commas in thousands).
Example:
FormatCurrency(profit)
txtFee.Text = FormatCurrency(7.5)
Empty String Validation:
If textBox.text <> String.Empty Then
Format String:
String.Format (“{0:F}”, average)
‘ average is formatted to floating point
String.Format (“{0:C}”, amount)
‘ amount is formatted to currency
Val Function: Ensures that value returned from a string is a number, anything other than number will
return a zero.
Dim principal As Decimal = Val (txtPrincipal.text)
Defining Variables and Constants
Dim i, n As Integer
Dim ytdEarnings, curEarnings As Double
Dim president As String
Defining and initializing variables
Dim NumberOne As Integer = 100
Dim president As String = “John Wilson”
Dim firstName As String = txtBox.Text
Defining constants
Const WAGE_BASE As Double = 106800
Converting Decimal to Binary, Octal, Decimal, and Hexadecimal
‘The following program reads a decimal number froma text box and converts and writes the
result in various base format to a list box. A general conversion function is used to
convert a decimal number to a desired number in the baser format: Dim desiredNumber As
String = Convert.ToString(decimalNumber, base)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim decimalNumber As Integer = TextBox1.Text
Dim
Dim
Dim
Dim
binaryNumber As String = Convert.ToString(decimalNumber, 2)
OctalNumber As String = Convert.ToString(decimalNumber, 8)
deciNumber As String = Convert.ToString(decimalNumber, 10)
hexadecimalNumber As String = Convert.ToString(decimalNumber, 16)
ListBox1.Items.Add(binaryNumber)
ListBox1.Items.Add(OctalNumber)
ListBox1.Items.Add(deciNumber)
ListBox1.Items.Add(hexadecimalNumber)
End Sub
Download