vb6parse 1.0.1

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="VB6Parse Library Reference - format - String">
    <title>format - String - VB6Parse Library Reference</title>
    <link rel="stylesheet" href="../../../assets/css/style.css">
    <link rel="stylesheet" href="../../../assets/css/docs-style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
    <script src="../../../assets/js/theme-switcher.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
    <script>hljs.highlightAll();</script>
</head>
<body>
    <header class="docs-header">
        <div class="container">
            <h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/functions/string/index.html">String</a> / format</h1>
            <p class="tagline">VB6 Library Reference</p>
        </div>
    </header>

    <nav class="docs-nav">
        <div class="container">
            <a href="../../../index.html">Home</a>
            <a href="../../../library/index.html">Library Reference</a>
            <a href="../../../documentation.html">Documentation</a>
            <a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
            <a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
            <button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
                <span class="theme-icon">🌙</span>
            </button>
        </div>
    </nav>

    <main class="container">
        
        <article class="library-item">
            <h1 id="format-function">Format Function</h1>
<p>Returns a <code>Variant</code> (<code>String</code>) containing an expression formatted according to instructions contained in a format expression.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Format(expression[, format[, firstdayofweek[, firstweekofyear]]])</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><strong>expression</strong>: Required. Any valid expression to be formatted.</li>
<li><strong>format</strong>: Optional. A valid named or user-defined format expression.</li>
<li><strong>firstdayofweek</strong>: Optional. A constant that specifies the first day of the week (vbSunday=1, vbMonday=2, etc.).</li>
<li><strong>firstweekofyear</strong>: Optional. A constant that specifies the first week of the year.</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a <code>Variant</code> of subtype <code>String</code> containing the formatted expression. If format is omitted,
<code>Format</code> returns a string similar to the <code>Str</code> function.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>Format</code> function is one of VB6's most versatile functions for converting values to
formatted strings. It supports numeric formatting, date/time formatting, and string formatting
with extensive control over output appearance.
<strong>Important Characteristics:</strong>
- Supports named formats (e.g., "General Number", "Currency", "Short Date")
- Supports user-defined format strings with placeholders
- Numeric formats: 0, #, ., ,, %, E, etc.
- Date/time formats: d, m, y, h, n, s, etc.
- String formats: @, &amp;, &lt;, &gt;, !
- Returns empty string if expression is Null
- Locale-aware (respects regional settings)
- Can create custom formats with multiple sections
- Supports scientific notation
- Handles positive, negative, zero, and null values separately</p>
<h2 id="named-formats">Named Formats</h2>
<h3 id="numeric-named-formats">Numeric Named Formats</h3>
<ul>
<li><strong>General Number</strong>: No thousand separator; displays as entered</li>
<li><strong>Currency</strong>: Thousand separator, 2 decimal places, currency symbol</li>
<li><strong>Fixed</strong>: At least one digit left of decimal, 2 digits right</li>
<li><strong>Standard</strong>: Thousand separator, 2 decimal places</li>
<li><strong>Percent</strong>: Multiplies by 100, adds percent sign, 2 decimal places</li>
<li><strong>Scientific</strong>: Standard scientific notation</li>
</ul>
<h3 id="datetime-named-formats">Date/Time Named Formats</h3>
<ul>
<li><strong>General Date</strong>: Short date and time if time ≠ midnight</li>
<li><strong>Long Date</strong>: Full date (e.g., "Wednesday, January 1, 2025")</li>
<li><strong>Medium Date</strong>: Date with abbreviated month (e.g., "01-Jan-25")</li>
<li><strong>Short Date</strong>: Locale-specific short date (e.g., "1/1/2025")</li>
<li><strong>Long Time</strong>: Full time with seconds (e.g., "1:30:00 PM")</li>
<li><strong>Medium Time</strong>: Time in 12-hour format (e.g., "1:30 PM")</li>
<li><strong>Short Time</strong>: Time in 24-hour format (e.g., "13:30")</li>
</ul>
<h2 id="typical-uses">Typical Uses</h2>
<ul>
<li>Format numbers with specific decimal places</li>
<li>Add thousand separators to large numbers</li>
<li>Format currency values</li>
<li>Display percentages</li>
<li>Format dates and times for display</li>
<li>Align text in reports</li>
<li>Create fixed-width output</li>
<li>Zero-pad numbers</li>
<li>Convert values to specific string representations</li>
</ul>
<h2 id="examples">Examples</h2>
<h3 id="basic-numeric-formatting">Basic Numeric Formatting</h3>
<pre><code class="language-vbnet">Dim value As Double
value = 1234.567
&#x27; Named formats
Debug.Print Format(value, &quot;General Number&quot;)  &#x27; 1234.567
Debug.Print Format(value, &quot;Currency&quot;)        &#x27; $1,234.57
Debug.Print Format(value, &quot;Fixed&quot;)           &#x27; 1234.57
Debug.Print Format(value, &quot;Standard&quot;)        &#x27; 1,234.57
Debug.Print Format(value, &quot;Percent&quot;)         &#x27; 123456.70%
Debug.Print Format(value, &quot;Scientific&quot;)      &#x27; 1.23E+03
&#x27; Custom formats
Debug.Print Format(value, &quot;0.00&quot;)            &#x27; 1234.57
Debug.Print Format(value, &quot;#,##0.00&quot;)        &#x27; 1,234.57
Debug.Print Format(value, &quot;000000.00&quot;)       &#x27; 001234.57</code></pre>
<h3 id="datetime-formatting">Date/Time Formatting</h3>
<pre><code class="language-vbnet">Dim dt As Date
dt = #1/15/2025 3:45:30 PM#
&#x27; Named formats
Debug.Print Format(dt, &quot;General Date&quot;)       &#x27; 1/15/2025 3:45:30 PM
Debug.Print Format(dt, &quot;Long Date&quot;)          &#x27; Wednesday, January 15, 2025
Debug.Print Format(dt, &quot;Short Date&quot;)         &#x27; 1/15/2025
Debug.Print Format(dt, &quot;Long Time&quot;)          &#x27; 3:45:30 PM
Debug.Print Format(dt, &quot;Short Time&quot;)         &#x27; 15:45
&#x27; Custom formats
Debug.Print Format(dt, &quot;yyyy-mm-dd&quot;)         &#x27; 2025-01-15
Debug.Print Format(dt, &quot;dd/mm/yyyy&quot;)         &#x27; 15/01/2025
Debug.Print Format(dt, &quot;hh:nn:ss&quot;)           &#x27; 03:45:30
Debug.Print Format(dt, &quot;dddd, mmmm d, yyyy&quot;) &#x27; Wednesday, January 15, 2025</code></pre>
<h3 id="string-formatting">String Formatting</h3>
<pre><code class="language-vbnet">Dim text As String
text = &quot;hello&quot;
Debug.Print Format(text, &quot;&gt;&quot;)                &#x27; HELLO (uppercase)
Debug.Print Format(text, &quot;&lt;&quot;)                &#x27; hello (lowercase)
Debug.Print Format(text, &quot;@@@@@@@@@@&quot;)       &#x27; 00000hello (right-aligned)</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="format-currency-with-symbol">Format Currency with Symbol</h3>
<pre><code class="language-vbnet">Function FormatCurrency(amount As Double) As String
    FormatCurrency = Format(amount, &quot;$#,##0.00&quot;)
End Function
&#x27; Usage
Debug.Print FormatCurrency(1234.56)  &#x27; $1,234.56
Debug.Print FormatCurrency(-500)     &#x27; $-500.00</code></pre>
<h3 id="zero-pad-numbers">Zero-Pad Numbers</h3>
<pre><code class="language-vbnet">Function PadWithZeros(num As Long, totalDigits As Integer) As String
    Dim formatStr As String
    formatStr = String(totalDigits, &quot;0&quot;)
    PadWithZeros = Format(num, formatStr)
End Function
&#x27; Usage
Debug.Print PadWithZeros(42, 6)      &#x27; 000042
Debug.Print PadWithZeros(7, 3)       &#x27; 007</code></pre>
<h3 id="format-file-sizes">Format File Sizes</h3>
<pre><code class="language-vbnet">Function FormatFileSize(bytes As Long) As String
    Const KB = 1024
    Const MB = 1048576
    Const GB = 1073741824
    If bytes &gt;= GB Then
        FormatFileSize = Format(bytes / GB, &quot;0.00&quot;) &amp; &quot; GB&quot;
    ElseIf bytes &gt;= MB Then
        FormatFileSize = Format(bytes / MB, &quot;0.00&quot;) &amp; &quot; MB&quot;
    ElseIf bytes &gt;= KB Then
        FormatFileSize = Format(bytes / KB, &quot;0.00&quot;) &amp; &quot; KB&quot;
    Else
        FormatFileSize = Format(bytes, &quot;#,##0&quot;) &amp; &quot; bytes&quot;
    End If
End Function</code></pre>
<h3 id="format-phone-numbers">Format Phone Numbers</h3>
<pre><code class="language-vbnet">Function FormatPhoneNumber(phoneNum As String) As String
    &#x27; Remove non-numeric characters
    Dim cleaned As String
    Dim i As Long
    For i = 1 To Len(phoneNum)
        If IsNumeric(Mid(phoneNum, i, 1)) Then
            cleaned = cleaned &amp; Mid(phoneNum, i, 1)
        End If
    Next i
    &#x27; Format as (XXX) XXX-XXXX
    If Len(cleaned) = 10 Then
        FormatPhoneNumber = &quot;(&quot; &amp; Left(cleaned, 3) &amp; &quot;) &quot; &amp; _
                            Mid(cleaned, 4, 3) &amp; &quot;-&quot; &amp; _
                            Right(cleaned, 4)
    Else
        FormatPhoneNumber = phoneNum
    End If
End Function</code></pre>
<h3 id="custom-date-display">Custom Date Display</h3>
<pre><code class="language-vbnet">Function FormatDateFriendly(dt As Date) As String
    Dim daysDiff As Long
    daysDiff = DateDiff(&quot;d&quot;, dt, Date)
    Select Case daysDiff
        Case 0
            FormatDateFriendly = &quot;Today at &quot; &amp; Format(dt, &quot;h:nn AM/PM&quot;)
        Case 1
            FormatDateFriendly = &quot;Yesterday at &quot; &amp; Format(dt, &quot;h:nn AM/PM&quot;)
        Case 2 To 6
            FormatDateFriendly = Format(dt, &quot;dddd&quot;) &amp; &quot; at &quot; &amp; _
                                 Format(dt, &quot;h:nn AM/PM&quot;)
        Case Else
            FormatDateFriendly = Format(dt, &quot;mmmm d, yyyy&quot;)
    End Select
End Function</code></pre>
<h3 id="format-percentages">Format Percentages</h3>
<pre><code class="language-vbnet">Function FormatPercent(value As Double, decimals As Integer) As String
    Dim formatStr As String
    formatStr = &quot;0.&quot; &amp; String(decimals, &quot;0&quot;) &amp; &quot;%&quot;
    FormatPercent = Format(value, formatStr)
End Function
&#x27; Usage
Debug.Print FormatPercent(0.1234, 2)   &#x27; 12.34%
Debug.Print FormatPercent(0.5, 0)      &#x27; 50%</code></pre>
<h3 id="format-decimal-places">Format Decimal Places</h3>
<pre><code class="language-vbnet">Function FormatDecimal(value As Double, places As Integer) As String
    Dim formatStr As String
    If places = 0 Then
        formatStr = &quot;0&quot;
    Else
        formatStr = &quot;0.&quot; &amp; String(places, &quot;0&quot;)
    End If
    FormatDecimal = Format(value, formatStr)
End Function
&#x27; Usage
Debug.Print FormatDecimal(3.14159, 2)  &#x27; 3.14
Debug.Print FormatDecimal(100.7, 0)    &#x27; 101</code></pre>
<h3 id="align-text-in-reports">Align Text in Reports</h3>
<pre><code class="language-vbnet">Sub PrintReport()
    Debug.Print &quot;Name&quot;, &quot;Amount&quot;
    Debug.Print String(40, &quot;-&quot;)
    Debug.Print &quot;Item 1&quot;, Format(1234.56, &quot;$#,##0.00&quot;)
    Debug.Print &quot;Item 2&quot;, Format(78.9, &quot;$#,##0.00&quot;)
    Debug.Print &quot;Item 3&quot;, Format(10000, &quot;$#,##0.00&quot;)
End Sub</code></pre>
<h3 id="format-elapsed-time">Format Elapsed Time</h3>
<pre><code class="language-vbnet">Function FormatElapsedTime(seconds As Long) As String
    Dim hours As Long
    Dim minutes As Long
    Dim secs As Long
    hours = seconds \ 3600
    minutes = (seconds Mod 3600) \ 60
    secs = seconds Mod 60
    FormatElapsedTime = Format(hours, &quot;00&quot;) &amp; &quot;:&quot; &amp; _
                        Format(minutes, &quot;00&quot;) &amp; &quot;:&quot; &amp; _
                        Format(secs, &quot;00&quot;)
End Function
&#x27; Usage
Debug.Print FormatElapsedTime(3661)    &#x27; 01:01:01</code></pre>
<h3 id="scientific-notation">Scientific Notation</h3>
<pre><code class="language-vbnet">Function FormatScientific(value As Double, decimals As Integer) As String
    Dim formatStr As String
    formatStr = &quot;0.&quot; &amp; String(decimals, &quot;0&quot;) &amp; &quot;E+00&quot;
    FormatScientific = Format(value, formatStr)
End Function
&#x27; Usage
Debug.Print FormatScientific(1234567, 2)  &#x27; 1.23E+06</code></pre>
<h3 id="conditional-formatting">Conditional Formatting</h3>
<pre><code class="language-vbnet">Function FormatNumber(value As Double) As String
    &#x27; Format: positive;negative;zero;null
    FormatNumber = Format(value, &quot;$#,##0.00;($#,##0.00);-;N/A&quot;)
End Function
&#x27; Usage
Debug.Print FormatNumber(1234.5)    &#x27; $1,234.50
Debug.Print FormatNumber(-500)      &#x27; ($500.00)
Debug.Print FormatNumber(0)         &#x27; -</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="multi-section-format-strings">Multi-Section Format Strings</h3>
<pre><code class="language-vbnet">&#x27; Format: positive;negative;zero;null
Function FormatWithSigns(value As Variant) As String
    If IsNull(value) Then
        FormatWithSigns = &quot;N/A&quot;
    Else
        FormatWithSigns = Format(value, &quot;+#,##0.00;-#,##0.00;Zero&quot;)
    End If
End Function
&#x27; Color-coded text (for display in rich text)
Function FormatColorCoded(value As Double) As String
    &#x27; Positive=green, Negative=red, Zero=black
    If value &gt; 0 Then
        FormatColorCoded = &quot;[Green]&quot; &amp; Format(value, &quot;$#,##0.00&quot;)
    ElseIf value &lt; 0 Then
        FormatColorCoded = &quot;[Red]&quot; &amp; Format(value, &quot;($#,##0.00)&quot;)
    Else
        FormatColorCoded = &quot;[Black]$0.00&quot;
    End If
End Function</code></pre>
<h3 id="invoice-number-formatting">Invoice Number Formatting</h3>
<pre><code class="language-vbnet">Function FormatInvoiceNumber(invoiceNum As Long, prefix As String) As String
    FormatInvoiceNumber = prefix &amp; &quot;-&quot; &amp; Format(invoiceNum, &quot;000000&quot;)
End Function
&#x27; Usage
Debug.Print FormatInvoiceNumber(123, &quot;INV&quot;)    &#x27; INV-000123
Debug.Print FormatInvoiceNumber(45678, &quot;PO&quot;)   &#x27; PO-045678</code></pre>
<h3 id="custom-number-format-builder">Custom Number Format Builder</h3>
<pre><code class="language-vbnet">Function BuildNumberFormat(decimals As Integer, _
                           Optional useSeparator As Boolean = True, _
                           Optional prefix As String = &quot;&quot;, _
                           Optional suffix As String = &quot;&quot;) As String
    Dim formatStr As String
    &#x27; Build integer part
    If useSeparator Then
        formatStr = &quot;#,##0&quot;
    Else
        formatStr = &quot;0&quot;
    End If
    &#x27; Add decimal part
    If decimals &gt; 0 Then
        formatStr = formatStr &amp; &quot;.&quot; &amp; String(decimals, &quot;0&quot;)
    End If
    &#x27; Add prefix/suffix
    formatStr = prefix &amp; formatStr &amp; suffix
    BuildNumberFormat = formatStr
End Function
&#x27; Usage
Debug.Print Format(1234.5, BuildNumberFormat(2, True, &quot;$&quot;, &quot;&quot;))  &#x27; $1,234.50</code></pre>
<h3 id="format-for-sql-queries">Format for SQL Queries</h3>
<pre><code class="language-vbnet">Function FormatForSQL(value As Variant) As String
    Select Case VarType(value)
        Case vbString
            &#x27; Escape single quotes
            FormatForSQL = &quot;&#x27;&quot; &amp; Replace(CStr(value), &quot;&#x27;&quot;, &quot;&#x27;&#x27;&quot;) &amp; &quot;&#x27;&quot;
        Case vbDate
            &#x27; Format as ISO date
            FormatForSQL = &quot;&#x27;&quot; &amp; Format(value, &quot;yyyy-mm-dd hh:nn:ss&quot;) &amp; &quot;&#x27;&quot;
        Case vbNull
            FormatForSQL = &quot;NULL&quot;
        Case vbBoolean
            If CBool(value) Then
                FormatForSQL = &quot;1&quot;
            Else
                FormatForSQL = &quot;0&quot;
            End If
        Case Else
            FormatForSQL = CStr(value)
    End Select
End Function</code></pre>
<h3 id="dynamic-column-width-formatting">Dynamic Column Width Formatting</h3>
<pre><code class="language-vbnet">Function FormatColumn(value As Variant, width As Integer, _
                      Optional align As String = &quot;left&quot;) As String
    Dim formatted As String
    formatted = CStr(value)
    If Len(formatted) &gt; width Then
        formatted = Left(formatted, width - 3) &amp; &quot;...&quot;
    Else
        Select Case LCase(align)
            Case &quot;right&quot;
                formatted = Space(width - Len(formatted)) &amp; formatted
            Case &quot;center&quot;
                Dim leftPad As Integer
                leftPad = (width - Len(formatted)) \ 2
                formatted = Space(leftPad) &amp; formatted &amp; _
                            Space(width - Len(formatted) - leftPad)
            Case Else  &#x27; left
                formatted = formatted &amp; Space(width - Len(formatted))
        End Select
    End If
    FormatColumn = formatted
End Function</code></pre>
<h3 id="localized-datetime-formatting">Localized Date/Time Formatting</h3>
<pre><code class="language-vbnet">Function FormatLocalizedDate(dt As Date, style As String) As String
    &#x27; Uses system locale settings
    Select Case LCase(style)
        Case &quot;short&quot;
            FormatLocalizedDate = Format(dt, &quot;Short Date&quot;)
        Case &quot;long&quot;
            FormatLocalizedDate = Format(dt, &quot;Long Date&quot;)
        Case &quot;iso&quot;
            FormatLocalizedDate = Format(dt, &quot;yyyy-mm-dd&quot;)
        Case &quot;filename&quot;
            &#x27; Safe for filenames
            FormatLocalizedDate = Format(dt, &quot;yyyy-mm-dd_hhnnss&quot;)
        Case Else
            FormatLocalizedDate = Format(dt, &quot;General Date&quot;)
    End Select
End Function</code></pre>
<h2 id="error-handling">Error Handling</h2>
<pre><code class="language-vbnet">Function SafeFormat(value As Variant, formatStr As String) As String
    On Error GoTo ErrorHandler
    If IsNull(value) Then
        SafeFormat = &quot;&quot;
    Else
        SafeFormat = Format(value, formatStr)
    End If
    Exit Function
ErrorHandler:
    Select Case Err.Number
        Case 13  &#x27; Type mismatch
            Debug.Print &quot;Format error: Invalid data type for format string&quot;
            SafeFormat = CStr(value)
        Case 5   &#x27; Invalid procedure call
            Debug.Print &quot;Format error: Invalid format string&quot;
            SafeFormat = CStr(value)
        Case Else
            Debug.Print &quot;Format error &quot; &amp; Err.Number &amp; &quot;: &quot; &amp; Err.Description
            SafeFormat = &quot;&quot;
    End Select
End Function</code></pre>
<h3 id="common-errors">Common Errors</h3>
<ul>
<li><strong>Error 13</strong> (Type Mismatch): Value type incompatible with format string</li>
<li><strong>Error 5</strong> (Invalid procedure call): Invalid format string syntax</li>
<li><strong>Error 6</strong> (Overflow): Value too large for format</li>
</ul>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li>Format is relatively fast for simple conversions</li>
<li>Complex format strings take longer to parse</li>
<li>Avoid calling Format in tight loops if possible</li>
<li>Consider caching format strings used repeatedly</li>
<li>String concatenation with Format can be slow</li>
<li>For simple conversions, CStr/Str may be faster</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<h3 id="use-named-formats-when-possible">Use Named Formats When Possible</h3>
<pre><code class="language-vbnet">&#x27; Good - Clear and locale-aware
formatted = Format(amount, &quot;Currency&quot;)
&#x27; Less portable - Hard-coded currency symbol
formatted = Format(amount, &quot;$#,##0.00&quot;)</code></pre>
<h3 id="store-format-strings-as-constants">Store Format Strings as Constants</h3>
<pre><code class="language-vbnet">Const FMT_CURRENCY = &quot;$#,##0.00&quot;
Const FMT_PERCENT = &quot;0.00%&quot;
Const FMT_DATE_ISO = &quot;yyyy-mm-dd&quot;
formatted = Format(value, FMT_CURRENCY)</code></pre>
<h3 id="handle-null-values">Handle Null Values</h3>
<pre><code class="language-vbnet">&#x27; Good - Check for Null
If Not IsNull(value) Then
    formatted = Format(value, &quot;0.00&quot;)
Else
    formatted = &quot;N/A&quot;
End If</code></pre>
<h2 id="comparison-with-other-functions">Comparison with Other Functions</h2>
<h3 id="format-vs-formatnumber"><code>Format</code> vs <code>FormatNumber</code></h3>
<pre><code class="language-vbnet">&#x27; Format - More flexible, custom formats
result = Format(1234.567, &quot;#,##0.00&quot;)
&#x27; FormatNumber - Simpler, fewer options
result = FormatNumber(1234.567, 2)</code></pre>
<h3 id="format-vs-formatcurrency"><code>Format</code> vs <code>FormatCurrency</code></h3>
<pre><code class="language-vbnet">&#x27; Format - Custom currency format
result = Format(1234.56, &quot;$#,##0.00&quot;)
&#x27; FormatCurrency - Uses system locale
result = FormatCurrency(1234.56)</code></pre>
<h3 id="format-vs-formatdatetime"><code>Format</code> vs <code>FormatDateTime</code></h3>
<pre><code class="language-vbnet">&#x27; Format - Custom date format
result = Format(Now, &quot;yyyy-mm-dd hh:nn:ss&quot;)
&#x27; FormatDateTime - Named formats only
result = FormatDateTime(Now, vbShortDate)</code></pre>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Format strings are not validated until runtime</li>
<li>Limited regex or pattern matching capabilities</li>
<li>Cannot format arrays or objects directly</li>
<li>No built-in support for custom cultures</li>
<li>Some format characters are locale-dependent</li>
<li>Return value is always <code>String</code> (<code>Variant</code>)</li>
</ul>
<h2 id="format-string-reference">Format String Reference</h2>
<h3 id="numeric-format-characters">Numeric Format Characters</h3>
<ul>
<li><strong>0</strong>: Digit placeholder (displays 0 if no digit)</li>
<li><strong>#</strong>: Digit placeholder (displays nothing if no digit)</li>
<li><strong>.</strong>: Decimal placeholder</li>
<li><strong>,</strong>: Thousand separator</li>
<li><strong>%</strong>: Percentage placeholder (multiplies by 100)</li>
<li><strong>E+</strong>, <strong>E-</strong>, <strong>e+</strong>, <strong>e-</strong>: Scientific notation</li>
</ul>
<h3 id="datetime-format-characters">Date/Time Format Characters</h3>
<ul>
<li><strong>d</strong>, <strong>dd</strong>: Day (1-31, 01-31)</li>
<li><strong>ddd</strong>, <strong>dddd</strong>: Day name (Mon, Monday)</li>
<li><strong>m</strong>, <strong>mm</strong>: Month (1-12, 01-12)</li>
<li><strong>mmm</strong>, <strong>mmmm</strong>: Month name (Jan, January)</li>
<li><strong>yy</strong>, <strong>yyyy</strong>: Year (25, 2025)</li>
<li><strong>h</strong>, <strong>hh</strong>: Hour (0-23, 00-23)</li>
<li><strong>n</strong>, <strong>nn</strong>: Minute (0-59, 00-59)</li>
<li><strong>s</strong>, <strong>ss</strong>: Second (0-59, 00-59)</li>
<li><strong>AM/PM</strong>, <strong>am/pm</strong>: 12-hour time indicator</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>FormatNumber</code>: Formats a number with specified decimal places</li>
<li><code>FormatCurrency</code>: Formats a number as currency</li>
<li><code>FormatPercent</code>: Formats a number as percentage</li>
<li><code>FormatDateTime</code>: Formats a date/time value</li>
<li><code>Str</code>: Converts number to string</li>
<li><code>CStr</code>: Converts expression to string</li>
</ul>
        </article>
        
        <div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
            <p>
                <a href="index.html">← Back to String</a> |
                <a href="../index.html">View all functions</a>
            </p>
        </div>

    </main>

    <footer>
        <div class="container">
            <p>&copy; 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
        </div>
    </footer>
</body>
</html>