Dim strFName As String = "Fred" Dim strLName As String = "Flintstone" Console.WriteLine (strLName & ", " & strFName)which would produce the single string :
Flintstone, Fredin the console window. Notice here there are three strings being combined; the string "Flintstone", the string ", " and the string "Fred", however each concatenation operator is still working on two operands since it is a binary operator.
The FormatNumber method is used to format an expression as a general number in some particular way, such as the number of places before or after the decimal point. The value returned from the FormatNumber method is a String type. General syntax (in part) looks like:
FormatNumber (expression [,NumDigitsAfterDecimal][,IncludeLeadingDigit])where:
decAnswer = 123.45D txtAnswer.Text = FormatNumber(decAnswer, 3) 'results in (String) 123.450
The FormatCurrency method is used to format an expression as a currency value in some particular way, with a leading currency symbol (e.g. a $ in the U.S.) and perhaps the number of places before or after the decimal point. The value returned from the FormatCurrency method is a String type. General syntax (in part) looks like:
FormatCurrency (expression [,NumDigitsAfterDecimal][,IncludeLeadingDigit])where, as above:
decAnswer = 123.45D txtAnswer.Text = FormatCurrency(decAnswer) 'results in (String) $123.45or looking at another example
decAnswer = 0.45D txtAnswer.Text = FormatCurrency(decAnswer,,TriState.False) 'results in (String) $.45
There are often times we would like to convert a non-string data type to a String type for output formatting. For example, we might want to concatenate a String type with a Decimal value, or assign a Decimal value to a TextBox. One way to do this is using the .ToString method. Most, if not all primitive data types support the .ToString method. An example of this is as follows:
txtOutput.Text = decOutput.ToStringHopefully, it is obvious that this method returns a String data type.
Below is some VB code to further exemplify formatting examples using the .ToString method. These use the Console.WriteLine method for simplicity, however these will also work when formatting control objects such as textboxes.
Dim myDecNumber As Decimal = 12345.678D Console.WriteLine(vbCrLf & "No formatting: " & myDecNumber.ToString & vbCrLf) 'Demonstrate the standard formats Console.WriteLine("Standard Formats:" + vbCrLf) Console.WriteLine(" Currency formatting: " & myDecNumber.ToString("C")) Console.WriteLine(" Exponential formatting: " & myDecNumber.ToString("E")) Console.WriteLine(" Fixed-point formatting: " & myDecNumber.ToString("F2")) Console.WriteLine(" General formatting: " & myDecNumber.ToString("G")) Console.WriteLine(" Number formatting to 2 decimal places: " & myDecNumber.ToString("N2")) Console.WriteLine(" Number formatting to 3 decimal places: " & myDecNumber.ToString("N3")) Console.WriteLine(" Number formatting to 4 decimal places: " & myDecNumber.ToString("N4")) Console.WriteLine(" Percent formatting: " & myDecNumber.ToString("P0"))The output of from the above code appears as follows:
No formatting: 12345.678 Standard Formats: Currency formatting: $12,345.68 Exponential formatting: 1.234568E+004 Fixed-point formatting: 12345.68 General formatting: 12345.678 Number formatting to 2 decimal places: 12,345.68 Number formatting to 3 decimal places: 12,345.678 Number formatting to 4 decimal places: 12,345.6780 Percent formatting: 1,234,568 %See additional information from Microsoft on standard numeric formatting here.
1 The TriState type is a ternary type (i.e. three values) whose values
consist of TriState.True, TriState.False, and Tristate.UseDefault. TriState.True yields a True value, TriState.False
yields a False value, and TriState.UseDefault uses the computers regional settings.
String Manipulation
Next Section: Procedures | Table of Contents |
©2007, Mark A. Thomas. All Rights Reserved.