My favorites | Sign in
Project Logo
                
Search
for
Updated Jun 22, 2008 by woollybah
FormatModule  
C-style printf String Formatting

Introduction

printf syntax is a universally recognized method of formatted strings. This module attempts to implement the most-used conversion specifications.

Requirements

The Format module requires the RegExModule. You can get it from the Downloads section.

A Quick Guide to the 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 (see below). In between, and in the following order, there may be zero or more flags, an optional field width, and an optional precision.

Flags

The % character is followed by zero or more flags :

Flag Description
# The value should be converted to an alternate form. For f conversions, the result will always contain a decimal point, even if no digits follow it. For other conversions, the result is undefined.
0 Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero. Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written. Used with g or G the result is the same as with e or E but trailing zeros are not removed.
- The converted value is to be left justified on the field boundary. (The default is right justification.) Except for n conversions, the converted value is padded on the right with blanks, rather than on the left with blanks or zeros. A - overrides a 0 if both are given.
' ' (space) A blank should be left before a positive number (or empty string) produced by a signed conversion.
+ A sign (+ or -) should always be placed before a number produced by a signed conversion. By default a sign is used only for negative numbers. A + overrides a space if both are used.

Field width

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.

Precision

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 integer (d, i, o, u, x, X) conversions, the number of digits to appear after the radix character for e, E and f conversions, or the maximum number of characters to be printed from a string for s conversions.

Conversion specifiers

Specifier Output Example
c Character a
d or i Signed decimal integer 392
e Scientific notation (mantise/exponent) using e character 3.9265e+2
E Scientific notation (mantise/exponent) using E character 3.9265E+2
f Decimal floating point 392.65
g Use the shorter of %e or %f 392.65
G Use the shorter of %E or %f 392.65
o Signed octal 610
s String of characters some text
u Unsigned decimal integer 4500
x Unsigned hexadecimal integer 5ac
X Unsigned hexadecimal integer (capital letters) 5AC
% A % followed by another % character will write a %.

Using the Formatter

You begin by creating a formatter, using the TFormatter type. The Create method takes a format String as a parameter. See the syntax guide below for formatting details.

Local formatter:TFormatter = TFormatter.Create("VAT = %2.1f%%")

Each conversion specification expects an argument (with the exception of %n and %%), to which you must supply an appropriate value. You do this by calling one of the following methods: ByteArg, ShortArg, IntArg, LongArg, FloatArg, DoubleArg or StringArg.

formatter.FloatArg(17.5)

Since each Arg method returns the TFormatter object, it allows you to tag together a sequence of arguments, like so:

Local formatter:TFormatter = TFormatter.Create("Name = %s : Id = X%09d")
formatter.StringArg("William Smith").IntArg(65002)

The Format method formats the text using the given arguments, and returns the String result.

Print formatter.format()

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.

Building

See HowToInstallModules to help you get started.


Sign in to add a comment
Hosted by Google Code