# String Formatting
Tools for complex string formatting and template replacement.
## Advanced Formatting
### Format
The `Format` function provides powerful template-based formatting similar to C's `printf` or Delphi's `Format`.
| Specifier | Description | Example |
| :--- | :--- | :--- |
| `%d` | Decimal Integer | `Format('%d', [42])` -> `42` |
| `%x` | Hexadecimal | `Format('%x', [255])` -> `FF` |
| `%f` | Floating point | `Format('%.2f', [3.1415])` -> `3.14` |
| `%e` | Scientific notation | `Format('%e', [1000])` -> `1.00E+003` |
| `%g` | General number | `Format('%g', [0.00001])` -> `1E-5` |
| `%n` | Number with separators | `Format('%n', [1234.5])` -> `1,234.50` |
| `%s` | String | `Format('Hi %s', ['Bob'])` -> `Hi Bob` |
| `%m` | Money | `Format('%m', [10.5])` -> `$10.50` |
```pascal
var s := Format(
'Total: %d items in %.2f seconds',
[ 10, 1.234 ]
);
PrintLn(s);
// OUTPUT
// Total: 10 items in 1.23 seconds
```
### FormatFloat
The `FormatFloat` function provides specialized formatting for floating-point numbers using a rich set of specifiers.
| Specifier | Description |
| :--- | :--- |
| `0` | Digit placeholder. If the value has a digit in this position, it is copied; otherwise, a '0' is stored. |
| `#` | Digit placeholder. If the value has a digit in this position, it is copied; otherwise, nothing is stored. |
| `.` | Decimal point. The first '.' in the format string determines the location of the decimal separator. |
| `,` | Thousand separator. If the format string contains one or more ',', the output will have thousand separators. |
| `E+` \/ `e+` | Scientific notation. Formats the number in exponential format. |
| `;` | Section separator. Separates the format string into positive, negative, and zero formats (e.g., `pos;neg;zero`). |
```pascal
var v := 1234.567;
// Fixed width and rounding
PrintLn(FormatFloat('00000.00', v)); // 01234.57
// Thousand separator and optional digits
PrintLn(FormatFloat('#,##0.##', v)); // 1,234.57
// Conditional formatting
var fmt := 'Income: #.##;Loss: (#.##);Balance: zero';
PrintLn(FormatFloat(fmt, -123.4)); // Loss: (123.4)
// OUTPUT
// 01234.57
// 1,234.57
// Loss: (123.4)
```
### Pattern Replacement
The `StrReplaceMacros` function (or `.ReplaceMacros` helper) allows for template-style replacements using an array of key-value pairs.
```pascal
var template := 'Hello [NAME], welcome to [PLACE].';
var data := ['NAME', 'Alice', 'PLACE', 'the Compendium'];
var msg := template.ReplaceMacros(data, '[', ']');
PrintLn(msg);
// OUTPUT
// Hello Alice, welcome to the Compendium.
```
String Formatting
Tools for complex string formatting and template replacement.
Advanced Formatting
Format
The Format function provides powerful template-based formatting similar to C's printf or Delphi's Format.
Specifier
Description
Example
%d
Decimal Integer
Format('%d', [42]) -> 42
%x
Hexadecimal
Format('%x', [255]) -> FF
%f
Floating point
Format('%.2f', [3.1415]) -> 3.14
%e
Scientific notation
Format('%e', [1000]) -> 1.00E+003
%g
General number
Format('%g', [0.00001]) -> 1E-5
%n
Number with separators
Format('%n', [1234.5]) -> 1,234.50
%s
String
Format('Hi %s', ['Bob']) -> Hi Bob
%m
Money
Format('%m', [10.5]) -> $10.50
var s := Format('Total: %d items in %.2f seconds',[10,1.234]);
PrintLn(s);
Result
Total: 10 items in 1.23 seconds
FormatFloat
The FormatFloat function provides specialized formatting for floating-point numbers using a rich set of specifiers.
Specifier
Description
0
Digit placeholder. If the value has a digit in this position, it is copied; otherwise, a '0' is stored.
#
Digit placeholder. If the value has a digit in this position, it is copied; otherwise, nothing is stored.
.
Decimal point. The first '.' in the format string determines the location of the decimal separator.
,
Thousand separator. If the format string contains one or more ',', the output will have thousand separators.
E+ / e+
Scientific notation. Formats the number in exponential format.
;
Section separator. Separates the format string into positive, negative, and zero formats (e.g., pos;neg;zero).
var v :=1234.567;// Fixed width and rounding
PrintLn(FormatFloat('00000.00', v));// 01234.57// Thousand separator and optional digits
PrintLn(FormatFloat('#,##0.##', v));// 1,234.57// Conditional formattingvar fmt :='Income: #.##;Loss: (#.##);Balance: zero';
PrintLn(FormatFloat(fmt,-123.4));// Loss: (123.4)
Result
01234.57
1,234.57
Loss: (123.4)
Pattern Replacement
The StrReplaceMacros function (or .ReplaceMacros helper) allows for template-style replacements using an array of key-value pairs.
var template :='Hello [NAME], welcome to [PLACE].';var data :=['NAME','Alice','PLACE','the Compendium'];var msg := template.ReplaceMacros(data,'[',']');
PrintLn(msg);