Other Formatting Functions
In addition to the formatting options we have seen previously (i.e., the Format function and the composite formatting feature), VB provides an additional set of four functions to format numbers and dates. These functions are:
FormatNumber |
Returns an expression formatted as a number |
FormatCurrency |
Returns an expression formatted as a number with a leading currency symbol ($) |
FormatPercent |
Returns an expression formatted as a percentage (multiplied by 100) with a trailing % character. |
FormatDateTime |
Returns an expression formatted as a date or time |
First, we will look at the three number formatting functions: FormatNumber, FormatCurrency, and FormatPercent:
The syntax for FormatNumber is:
FormatNumber(Expression[,NumDigitsAfterDecimal [,IncludeLeadingDigit [,UseParensForNegativeNumbers [,GroupDigits]]]])
Where:
Expression |
is the expression to be formatted |
NumDigitsAfterDecimal |
is the number of digits desired after the decimal point |
IncludeLeadingDigit |
is a True/False value indicating whether or not a zero should be included to the left of decimal values |
UseParensForNegativeNumbers |
is a True/False value indicating whether or not parentheses should be used for negative values |
GroupDigits |
is a True/False value indicating whether or not to use commas to group digits |
The syntax for FormatCurrency and FormatPercent are exactly the same as FormatNumber.
For the examples below, assume dblTestNumber contains the value 12345.678
Expression |
Result |
FormatNumber(dblTestNumber, 2, True, True, True) |
12,345.68 |
FormatCurrency(dblTestNumber, 2, True, True, True) |
$12,345.68 |
FormatPrecent(dblTestNumber, 2, True, True, True) |
1,234,567.80% |
To demonstrate the functions described above, create a new "Try It" project, and place the following code in Sub Main:
Dim strUserInput As String
Dim dblTestNumber As Double
Console.Write("Please enter a number: ")
strUserInput = Console.ReadLine()
dblTestNumber = Val(strUserInput)
Console.WriteLine()
Console.WriteLine("Using FormatNumber: " & FormatNumber(dblTestNumber, 2, True, True, True))
Console.WriteLine("Using FormatCurrency: " & FormatCurrency(dblTestNumber, 2, True, True, True))
Console.WriteLine("Using FormatPercent: " & FormatPercent(dblTestNumber, 2, True, True, True))
Console.ReadLine()
Run the project and enter 12345.678. The output will be displayed as shown in the screen shot on the right.
Test the code using a variety of different values, including negative numbers and zero.
|
Download the VB.NET project code for the example above here.
Now we will look at FormatDateTime.
The syntax for FormatDateTime is:
FormatDateTime(Date[,NamedFormat])
The FormatDateTime function syntax has these parts:
Part |
Description |
Date |
Required. Date expression to be formatted. |
NamedFormat |
Optional. Numeric value that indicates the date/time format used. If omitted, DateFormat.GeneralDate is used. |
Settings:
The NamedFormat
argument has the following settings:
Constant |
Value |
Description |
DateFormat.GeneralDate |
0 |
Display a date and/or time. If there is a date part, display it as a short date. If there is a time part, display it as a long time. If present, both parts are displayed. |
DateFormat.LongDate |
1 |
Display a date using the long date format specified in your computer's regional settings. |
DateFormat.ShortDate |
2 |
Display a date using the short date format specified in your computer's regional settings. |
DateFormat.LongTime |
3 |
Display a time using the time format specified in your computer's regional settings. |
DateFormat.ShortTime |
4 |
Display a time using the 24-hour format (hh:mm). |
To demonstrate the functions described above, create a new "Try It" project, and place the following code in Sub Main:
Console.WriteLine("Using GeneralDate: " & FormatDateTime(Now, DateFormat.GeneralDate))
Console.WriteLine("Using LongDate: " & FormatDateTime(Now, DateFormat.LongDate))
Console.WriteLine("Using ShortDate: " & FormatDateTime(Now, DateFormat.ShortDate))
Console.WriteLine("Using LongTime: " & FormatDateTime(Now, DateFormat.LongTime))
Console.WriteLine("Using ShortTime: " & FormatDateTime(Now, DateFormat.ShortTime))
Console.ReadLine()
Run the project to see the current date and time formatted in various ways.
|
Download the VB.NET project code for the example above here.