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
//! # `Str$` Function
//!
//! The `Str$` function in Visual Basic 6 converts a numeric value to a string representation.
//! The dollar sign (`$`) suffix indicates that this function always returns a `String` type,
//! never a `Variant`.
//!
//! ## Syntax
//!
//! ```vb6
//! Str$(number)
//! ```
//!
//! ## Parameters
//!
//! - `number` - Required. Any valid numeric expression. Can be of type `Byte`, `Integer`, `Long`,
//! `Single`, `Double`, or `Currency`.
//!
//! ## Return Value
//!
//! Returns a `String` representation of the number. Positive numbers include a leading space
//! for the sign position. Negative numbers include a leading minus sign (-).
//!
//! ## Behavior and Characteristics
//!
//! ### Sign Handling
//!
//! - Positive numbers: Include a leading space (e.g., " 123")
//! - Negative numbers: Include a leading minus sign (e.g., "-123")
//! - Zero: Returns " 0" (with leading space)
//! - The leading space reserves position for the sign
//!
//! ### Numeric Formatting
//!
//! - No thousands separators (e.g., "1000" not "1,000")
//! - Scientific notation for very large or very small numbers
//! - Floating-point numbers may show precision artifacts
//! - No control over decimal places
//!
//! ### Type Differences: `Str$` vs `Str`
//!
//! - `Str$`: Always returns `String` type (never `Variant`)
//! - `Str`: Returns `Variant` containing a string
//! - Use `Str$` when you need guaranteed `String` return type
//! - Use `Str` when working with `Variant` variables
//!
//! ## Common Usage Patterns
//!
//! ### 1. Basic Number to String Conversion
//!
//! ```vb6
//! Dim numStr As String
//! numStr = Str$(123) ' Returns " 123" (note leading space)
//! numStr = Str$(-45) ' Returns "-45"
//! ```
//!
//! ### 2. Concatenating Numbers with Text
//!
//! ```vb6
//! Function FormatMessage(count As Integer) As String
//! FormatMessage = "Found" & Str$(count) & " items"
//! End Function
//!
//! Debug.Print FormatMessage(5) ' "Found 5 items"
//! ```
//!
//! ### 3. Trimming the Leading Space
//!
//! ```vb6
//! Function NumberToString(value As Long) As String
//! NumberToString = LTrim$(Str$(value))
//! End Function
//!
//! Dim result As String
//! result = NumberToString(100) ' Returns "100" (no leading space)
//! ```
//!
//! ### 4. Building Comma-Separated Values
//!
//! ```vb6
//! Function BuildCSV(values() As Integer) As String
//! Dim i As Integer
//! Dim result As String
//! For i = LBound(values) To UBound(values)
//! If i > LBound(values) Then result = result & ","
//! result = result & LTrim$(Str$(values(i)))
//! Next i
//! BuildCSV = result
//! End Function
//! ```
//!
//! ### 5. Logging and Debug Output
//!
//! ```vb6
//! Sub LogValue(name As String, value As Double)
//! Debug.Print name & " =" & Str$(value)
//! End Sub
//! ```
//!
//! ### 6. Creating Numeric Labels
//!
//! ```vb6
//! Function CreateLabel(index As Integer) As String
//! CreateLabel = "Item" & LTrim$(Str$(index))
//! End Function
//!
//! Dim label As String
//! label = CreateLabel(42) ' Returns "Item42"
//! ```
//!
//! ### 7. File Output Formatting
//!
//! ```vb6
//! Sub WriteDataLine(fileNum As Integer, id As Long, amount As Currency)
//! Print #fileNum, LTrim$(Str$(id)) & "," & LTrim$(Str$(amount))
//! End Sub
//! ```
//!
//! ### 8. Array Index Display
//!
//! ```vb6
//! Sub ShowArrayContents(arr() As Integer)
//! Dim i As Integer
//! For i = LBound(arr) To UBound(arr)
//! Debug.Print "[" & LTrim$(Str$(i)) & "] = " & LTrim$(Str$(arr(i)))
//! Next i
//! End Sub
//! ```
//!
//! ### 9. Simple Calculator Display
//!
//! ```vb6
//! Function UpdateDisplay(value As Double) As String
//! UpdateDisplay = LTrim$(Str$(value))
//! End Function
//! ```
//!
//! ### 10. Building SQL Statements
//!
//! ```vb6
//! Function BuildQuery(userId As Long) As String
//! BuildQuery = "SELECT * FROM Users WHERE ID = " & LTrim$(Str$(userId))
//! End Function
//! ```
//!
//! ## Related Functions
//!
//! - `Str()` - Returns a `Variant` containing the string representation of a number
//! - `CStr()` - Converts an expression to a `String` (no leading space for positive numbers)
//! - `Format$()` - Provides extensive formatting control for numeric values
//! - `Val()` - Converts a string to a numeric value (inverse operation)
//! - `LTrim$()` - Removes leading spaces (often used with `Str$`)
//! - `Hex$()` - Converts a number to hexadecimal string
//! - `Oct$()` - Converts a number to octal string
//!
//! ## Best Practices
//!
//! ### When to Use `Str$` vs `CStr` vs `Format$`
//!
//! ```vb6
//! Dim value As Integer
//! value = 42
//!
//! ' Str$ includes leading space for positive numbers
//! Debug.Print Str$(value) ' " 42"
//!
//! ' CStr has no leading space
//! Debug.Print CStr(value) ' "42"
//!
//! ' Format$ provides control over formatting
//! Debug.Print Format$(value, "000") ' "042"
//! ```
//!
//! ### Always Trim for Display
//!
//! ```vb6
//! ' Without trim (has leading space for positive numbers)
//! Label1.Caption = Str$(count) ' " 5"
//!
//! ' With trim (clean output)
//! Label1.Caption = LTrim$(Str$(count)) ' "5"
//!
//! ' Or use CStr instead
//! Label1.Caption = CStr(count) ' "5"
//! ```
//!
//! ### Use `Format$` for Formatted Output
//!
//! ```vb6
//! ' Str$ has no formatting control
//! Debug.Print Str$(1234.5678) ' " 1234.5678"
//!
//! ' Format$ provides control
//! Debug.Print Format$(1234.5678, "#,##0.00") ' "1,234.57"
//! ```
//!
//! ### Handle Negative Numbers
//!
//! ```vb6
//! Function SafeConvert(value As Long) As String
//! ' Str$ handles negative numbers correctly
//! SafeConvert = LTrim$(Str$(value))
//! ' For negative: "-123", for positive: "123"
//! End Function
//! ```
//!
//! ## Performance Considerations
//!
//! - `Str$` is very fast for simple conversions
//! - Faster than `Format$` when formatting is not needed
//! - Similar performance to `CStr`
//! - No significant overhead for any numeric type
//!
//! ```vb6
//! ' Fast: simple conversion
//! For i = 1 To 10000
//! text = LTrim$(Str$(i))
//! Next i
//!
//! ' Slower: formatted conversion (but more control)
//! For i = 1 To 10000
//! text = Format$(i, "0000")
//! Next i
//! ```
//!
//! ## Common Pitfalls
//!
//! ### 1. Leading Space for Positive Numbers
//!
//! ```vb6
//! Dim result As String
//! result = Str$(100) ' " 100" (note the leading space!)
//!
//! ' This can cause problems in comparisons
//! If Str$(100) = "100" Then ' FALSE! (" 100" <> "100")
//! Debug.Print "Match"
//! End If
//!
//! ' Use LTrim$ or CStr instead
//! If LTrim$(Str$(100)) = "100" Then ' TRUE
//! Debug.Print "Match"
//! End If
//! ```
//!
//! ### 2. Confusion with `CStr`
//!
//! ```vb6
//! Dim value As Integer
//! value = 42
//!
//! Debug.Print Str$(value) ' " 42" (with space)
//! Debug.Print CStr(value) ' "42" (no space)
//!
//! ' Know which one you need
//! ```
//!
//! ### 3. No Formatting Control
//!
//! ```vb6
//! Dim amount As Currency
//! amount = 1234.56
//!
//! ' Str$ gives no control
//! Debug.Print Str$(amount) ' " 1234.56"
//!
//! ' Use Format$ for currency
//! Debug.Print Format$(amount, "$#,##0.00") ' "$1,234.56"
//! ```
//!
//! ### 4. Floating-Point Precision Issues
//!
//! ```vb6
//! Dim value As Double
//! value = 0.1 + 0.2
//!
//! Debug.Print Str$(value) ' May show " 0.30000000000000004"
//!
//! ' Use Format$ to control precision
//! Debug.Print Format$(value, "0.00") ' "0.30"
//! ```
//!
//! ### 5. Not Handling Very Large or Small Numbers
//!
//! ```vb6
//! Dim bigNum As Double
//! bigNum = 1E+20
//!
//! Debug.Print Str$(bigNum) ' " 1E+20" (scientific notation)
//!
//! ' Be aware of scientific notation in output
//! ```
//!
//! ### 6. Null Values
//!
//! ```vb6
//! ' Str$ cannot handle Null
//! Dim result As String
//! result = Str$(nullValue) ' Runtime error if nullValue is Null
//!
//! ' Check first
//! If Not IsNull(value) Then
//! result = Str$(value)
//! Else
//! result = ""
//! End If
//! ```
//!
//! ## Practical Examples
//!
//! ### Building a Progress Message
//!
//! ```vb6
//! Function ProgressMessage(current As Long, total As Long) As String
//! ProgressMessage = "Processing item" & Str$(current) & _
//! " of" & Str$(total)
//! End Function
//!
//! Debug.Print ProgressMessage(5, 10) ' "Processing item 5 of 10"
//! ```
//!
//! ### Creating Sequential Filenames
//!
//! ```vb6
//! Function GenerateFileName(baseNameStr As String, index As Integer) As String
//! GenerateFileName = baseNameStr & LTrim$(Str$(index)) & ".dat"
//! End Function
//!
//! Dim fileName As String
//! fileName = GenerateFileName("data", 1) ' "data1.dat"
//! ```
//!
//! ### Simple Data Export
//!
//! ```vb6
//! Sub ExportToCSV(data() As Double, fileName As String)
//! Dim i As Integer
//! Dim lineData As String
//!
//! Open fileName For Output As #1
//! For i = LBound(data) To UBound(data)
//! Print #1, LTrim$(Str$(data(i)))
//! Next i
//! Close #1
//! End Sub
//! ```
//!
//! ## Limitations
//!
//! - Always includes leading space for positive numbers (use `LTrim$` or `CStr` to remove)
//! - No formatting control (no thousands separators, decimal places, etc.)
//! - Cannot handle `Null` values (use `CStr` with error handling instead)
//! - May produce scientific notation for very large or small numbers
//! - Floating-point precision artifacts may appear in output
//! - No locale-specific formatting (always uses invariant format)
use crate::;
/// Converts a number to a string with a leading space for positive values.
/// The `$` suffix indicates this function returns a `String` type (not `Variant`).
///
/// Positive numbers (and zero) are prefixed with a single space for the sign
/// position; negative numbers are prefixed with a minus sign. Booleans convert
/// to `-1`/` 0` (not `True`/`False`). No thousands separators are added and the
/// decimal point is always `.`, regardless of locale.
///
///
/// # Errors
///
/// Returns error 13 (`Type mismatch`) when `number` is a non-numeric string or
/// an object.
///
/// `Str$` raises error 94 when `number` is `Null`; use `Str` for the
/// Null-propagating variant.