Format Function in Visual Basic 2022
Lesson 20 covers format function using the same proven VBTutor lesson layout, now adapted for Visual Basic 2022 and Visual Studio 2022.
Lesson Overview
Key Takeaway
This lesson focuses on format function in Visual Basic 2022.
Lesson 20: Format Function
The Format function provides both built-in and custom formatting options for numbers. It's essential for financial applications, reports, and any situation where data presentation matters.
Built-in Number Formats
VB2022 includes several predefined formats for common scenarios:
| Format | Description | Example |
|---|---|---|
| General Number | Displays number without thousands separators | 8972.234 |
| Fixed | Rounds to two decimal places | 8972.20 |
| Standard | Includes thousands separators | 6,648,972.27 |
| Currency | Adds currency symbol | $6,648,972.27 |
| Percent | Converts to percentage | 56.32% |
Private Sub BtnFormat_Click(sender As Object, e As EventArgs) Handles BtnFormat.Click ' Using built-in formats lblGeneral.Text = Format(8972.234, "General Number") lblFixed.Text = Format(8972.2, "Fixed") lblStandard.Text = Format(6648972.265, "Standard") lblCurrency.Text = Format(6648972.265, "Currency") lblPercent.Text = Format(0.56324, "Percent") End Sub
Custom Number Formats
Create specialized formats using pattern-based formatting:
| Format | Description | Example |
|---|---|---|
| 0.0 | Rounds to one decimal | 781234.6 |
| 0.00 | Rounds to two decimals | 781234.58 |
| #,##0.00 | Thousands separator | 781,234.58 |
| $#,##0.00 | Currency format | $781,234.58 |
| 0% | Percentage format | 58% |
Private Sub BtnCustomFormat_Click(sender As Object, e As EventArgs) Handles BtnCustomFormat.Click ' Using custom formats lblOneDecimal.Text = Format(8972.234, "0.0") lblTwoDecimals.Text = Format(8972.2345, "0.00") lblThousands.Text = Format(6648972.265, "#,##0.00") lblCurrency.Text = Format(6648972.265, "$#,##0.00") lblPercent.Text = Format(0.56324, "0.00%") End Sub
Special Number Formatting
Handle special cases like negative numbers, zero values, and custom patterns:
Private Sub BtnSpecialCases_Click(sender As Object, e As EventArgs) Handles BtnSpecialCases.Click ' Formatting negative numbers with parentheses lblNegative.Text = Format(-1234.56, "$#,##0.00;($#,##0.00)") ' Custom display for zero values lblZero.Text = Format(0, "$#,##0.00;""Zero"";""N/A""") ' Phone number formatting lblPhone.Text = Format(5551234567, "(###) ###-####") ' Social security number formatting lblSSN.Text = Format(123456789, "###-##-####") ' Custom alignment for decimal points lblAlign1.Text = Format(1234.5, "#####0.00") lblAlign2.Text = Format(78.9, "#####0.00") End Sub
Output Preview
Negative: ($1,234.56)
Zero: Zero
Phone: (555) 123-4567
SSN: 123-45-6789
Alignment: 1234.50
Alignment: 78.90
20.2 Formatting Dates and Times
Proper date and time formatting is crucial for international applications. VB2022 offers flexible options for displaying temporal data.
Built-in Date/Time Formats
Use predefined formats for common date/time presentations:
| Format | Description |
|---|---|
| General Date | Date and time (e.g., 6/17/2023 2:45 PM) |
| Long Date | Full date (e.g., Saturday, June 17, 2023) |
| Short Date | Numeric date (e.g., 6/17/2023) |
| Long Time | Time with seconds (e.g., 2:45:30 PM) |
| Short Time | Time without seconds (e.g., 14:45) |
Private Sub BtnDisplay_Click(sender As Object, e As EventArgs) Handles BtnDisplay.Click ' Display current date/time in various formats lblGeneral.Text = Format(Now, "General Date") lblLongDate.Text = Format(Now, "Long Date") lblShortDate.Text = Format(Now, "Short Date") lblLongTime.Text = Format(Now, "Long Time") lblShortTime.Text = Format(Now, "Short Time") End Sub
Custom Date/Time Formats
Create specialized date/time formats using pattern codes:
| Pattern | Description |
|---|---|
| d | Day of month (1-31) |
| dd | Day with leading zero (01-31) |
| ddd | Abbreviated day name (Mon, Tue) |
| dddd | Full day name (Monday) |
| M | Month number (1-12) |
| MM | Month with leading zero (01-12) |
| MMM | Abbreviated month name (Jan, Feb) |
| MMMM | Full month name (January) |
| yy | Two-digit year (23) |
| yyyy | Four-digit year (2023) |
Advanced Date Formatting
Combine patterns to create sophisticated date/time displays:
Private Sub BtnAdvancedDate_Click(sender As Object, e As EventArgs) Handles BtnAdvancedDate.Click ' Day of week formatting lblFullDay.Text = Format(Now, "dddd") ' Full day name lblShortDay.Text = Format(Now, "ddd") ' Abbreviated day name lblFullDate.Text = Format(Now, "dddd, MMMM d, yyyy") ' Business formats lblISO.Text = Format(Now, "yyyy-MM-dd") ' ISO format lbl12Hour.Text = Format(Now, "hh:mm tt") ' 12-hour format lbl24Hour.Text = Format(Now, "HH:mm") ' 24-hour format ' Financial quarter formatting Dim quarter As Integer = (Month(Now) + 2) \ 3 lblQuarter.Text = $"Q{quarter} {Year(Now)}" ' Countdown formatting Dim futureDate As Date = #12/31/2024# Dim timeLeft As TimeSpan = futureDate - Now lblCountdown.Text = $"{timeLeft.Days} days, {timeLeft.Hours} hours remaining" End Sub
Output Preview
Full Day: Saturday
Short Day: Sat
Full Date: Saturday, June 17, 2023
ISO Date: 2023-06-17
12 Hour: 02:45 PM
24 Hour: 14:45
Quarter: Q2 2023
Countdown: 197 days, 9 hours remaining
Real-time Clock Tip
To display continuously updating dates and times, use a Timer control with Enabled set to True and Interval set to 100:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick lblDateTime.Text = Format(Now, "dddd, MMMM d, yyyy h:mm:ss tt") lblCountdown.Text = Format(#12/31/2023# - Now, "d' days 'h' hours '") End Sub
Format Function Summary
The Format function is essential for professional data presentation:
| Category | Patterns | Examples |
|---|---|---|
| Numbers | 0, #, . , % | Format(1234.56, "#,##0.00") โ 1,234.56 |
| Currency | $ยฃโฌ, C | Format(1234.56, "Currency") โ $1,234.56 |
| Percent | % | Format(0.85, "Percent") โ 85.00% |
| Dates | d, M, yyyy | Format(Now, "MM/dd/yyyy") โ 06/17/2023 |
| Times | h, m, s, tt | Format(Now, "h:mm:ss tt") โ 2:45:30 PM |
| Custom | Combined patterns | Format(Now, "ddd, MMM d yyyy") โ Sat, Jun 17 2023 |
Consistency
Use consistent formatting throughout your application for professional results
Localization
Format function automatically adapts to system regional settings
Performance
For intensive formatting, cache formatted values instead of recalculating
Practical Exercises
Apply your formatting knowledge with these hands-on exercises:
Exercise 1: Financial Report Generator
Create a program that generates a financial report with properly formatted numbers:
- Format currency values with dollar signs and two decimal places
- Display percentages with one decimal place
- Align all decimal points in a column
- Format negative values in red with parentheses
Exercise 2: Event Countdown Timer
Build an application that counts down to a specific event:
- Display days, hours, minutes, and seconds remaining
- Format the countdown as "X days, Y hours, Z minutes"
- Update the display in real-time using a Timer control
- Change formatting as the event approaches
Exercise 3: International Date Formatter
Create a program that displays the current date in multiple formats:
- US format (MM/dd/yyyy)
- European format (dd/MM/yyyy)
- ISO format (yyyy-MM-dd)
- Long format (dddd, MMMM d, yyyy)
- Allow users to select their preferred format
Exercise 4: Data Export Utility
Develop an application that exports data to a CSV file with proper formatting:
- Format currency values without dollar signs
- Ensure dates are in ISO format for database compatibility
- Handle special characters in text fields
- Format percentages as decimals (e.g., 15% as 0.15)
Lesson Recap
Lesson Summary
You have completed Lesson 20: Format Function.
Core concept
You studied format function in VB2022.
Practical focus
The lesson follows the same classic VBTutor learning path and structure.
Modern tools
Examples are positioned around Visual Studio 2022 and VB.NET workflows.
Next step
Continue to Lesson 21 to keep progressing through the course.
Continue with the next lesson to build your VB2022 skills step by step.
Next Lesson
Ready to continue?
Lesson 21: CheckboxRelated Resources