VB6Parse / Library / String / format_dollar

VB6 Library Reference

Format$ Function

Returns a String formatted according to instructions contained in a format expression.

Syntax

Format$(expression[, format[, firstdayofweek[, firstweekofyear]]])

Parameters

Return Value

Returns a String containing the formatted representation of the expression. If format is omitted, Format$ returns a string similar to Str$.

Remarks

The Format$ function is one of the most versatile functions in VB6, allowing you to format numbers, dates, times, and strings according to predefined or custom format expressions.

You can use one of the predefined named formats or create user-defined formats with special characters that specify how the value should be displayed.

Named Numeric Formats

Named Date/Time Formats

User-Defined Number Format Characters

User-Defined Date/Time Format Characters

User-Defined String Format Characters

Typical Uses

Example 1: Formatting Currency

Dim amount As Double
amount = 1234.56
Text1.Text = Format$(amount, "Currency")  ' "$1,234.56"

Example 2: Custom Number Format

Dim value As Double
value = 1234.5
result = Format$(value, "0000.00")  ' "1234.50"

Example 3: Date Formatting

Dim today As Date
today = Now
dateStr = Format$(today, "Long Date")

Example 4: Custom Date Format

dateStr = Format$(Now, "yyyy-mm-dd")  ' "2024-01-15"

Common Usage Patterns

Formatting as Percentage

Dim rate As Double
rate = 0.075
display = Format$(rate, "0.00%")  ' "7.50%"

Zero-Padded Numbers

Dim id As Integer
id = 42
idStr = Format$(id, "000000")  ' "000042"

Phone Number Formatting

Dim phone As String
phone = "5551234567"
formatted = Format$(phone, "(@@@) @@@-@@@@")  ' "(555) 123-4567"

Time Formatting

Dim currentTime As Date
currentTime = Now
timeStr = Format$(currentTime, "hh:nn:ss AM/PM")

Scientific Notation

Dim bigNum As Double
bigNum = 12345678
sciStr = Format$(bigNum, "0.00E+00")  ' "1.23E+07"

File Timestamp

filename = "backup_" & Format$(Now, "yyyymmdd_hhnnss") & ".dat"

Accounting Format

balance = Format$(amount, "#,##0.00;(#,##0.00)")
' Positive: "1,234.56"
' Negative: "(1,234.56)"

Leading Zeros for Dates

monthStr = Format$(Month(Date), "00")  ' "01" to "12"
dayStr = Format$(Day(Date), "00")      ' "01" to "31"

Conditional Formatting

' Format: positive;negative;zero
result = Format$(value, "+0.00;-0.00;Zero")

Uppercase/Lowercase Conversion

upperName = Format$("john doe", ">")      ' "JOHN DOE"
lowerName = Format$("JOHN DOE", "<")      ' "john doe"

Best Practices

  1. Use named formats for common formatting tasks (clearer intent)
  2. Cache format strings if using the same format repeatedly
  3. Test custom format strings with edge cases (zero, negative, very large/small)
  4. Use @ instead of & in string formats when you want spaces preserved
  5. Remember that m vs mm depends on context (month vs minute)
  6. Use four-digit years (yyyy) to avoid Y2K-style issues
  7. Consider locale settings when using named formats
  8. Use semicolons to specify different formats for positive, negative, and zero
  9. Escape literal characters with backslash or quotes when needed
  10. Be aware that Format$ returns a string - convert back if needed

Performance Considerations

Locale Considerations

Aspect Behavior
Currency Symbol Uses system locale currency symbol
Decimal Separator Uses locale decimal separator (. or ,)
Thousand Separator Uses locale thousand separator
Date Format Named date formats use locale settings
Day/Month Names Uses locale language for names
AM/PM Designators Uses locale AM/PM strings
First Day of Week Can be overridden with parameter
First Week of Year Can be overridden with parameter

Common Pitfalls

Limitations

← Back to String | View all functions