The Format module is a String formatter using C-style sprintf / printf syntax.
printf syntax is a universally recognized method of formatted strings. This module attempts to implement the most-used conversion specifications.
Using the Formatter
Local formatter:TFormatter = TFormatter.Create("VAT = <i>2.1f</i>%%")
Each conversion specification expects an argument (with the exception of
%%), to which you must supply an appropriate value.
You do this by calling the Arg() method:
Local formatter:TFormatter = TFormatter.Create("Name = <i>s</i> : Id = X%09d") formatter.Arg("William Smith").Arg(65002)
The Format() method formats the text using the given arguments, and returns the String result.
To reuse a formatter, you can call the Clear() method to remove the arguments, allowing you to apply some new ones without the formatting engine having to re-process the format string.
A quick guide to syntax
The format string can contain both ordinary text and conversion specifications.
A conversion specification begins with a
% and ends with a particular conversion specifier (d, f, s, ).
In between, and in the following order, there may be zero or more flags, an optional field width, and an optional precision.
% character is followed by zero or more flags :
|The value should be converted to an alternate form. For |
For other conversions, the result is undefined.
|The value should be zero padded.|
For other conversions, the behaviour is undefined.
|The converted value is to be left justified on the field boundary. (The default is right justification.)|
|A blank should be left before a positive number (or empty string) produced by a signed conversion.|
|A sign (|
|For decimal conversion (|
An optional number specifying a minimum field width.
If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-justify flag has been given).
In no case does a non-existent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.
An optional precision, in the form of a period ('.') followed by an optional integer value.
If the precision is given as just '.', or the precision is negative, the precision is taken to be zero.
This gives the minimum number of digits to appear for
d conversions, the number of digits to appear after the radix character for
f conversions, or the maximum number of characters to be printed from a string for
A character that specifies the type of conversion to be applied. The conversion specifiers and their meanings are:
|The argument (#Byte, Short, Int, UInt, Long) is converted to signed decimal notation.|
The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded on the left with zeros.
The default precision is 1.
|The argument (#Float, Double) is rounded and converted to decimal notation in the style |
If the precision is missing, it is taken as 6; if the precision is explicitly zero, no decimal-point character appears.
If a decimal point appears, at least one digit appears before it.
|The argument (#String) is converted to a space-padded or left-justified string equal to the width specified.|
If precision is given, no more than the number of characters specified are output.
|A literal |
|TFormatter||The string formatter.|