Phantom Documentation

[Help Home] [Phantom Home]
format

string format(string,int)

Return Type: string
Parameters: string sFormat, int input

string format(string,real)

Return Type: string
Parameters: string sFormat, real input

Description:
This function formats an integer or a real to a string, similar to that of a C 'printf' statement. The sFormat string determines the format of the input in the returned string. sFormat can contain any text, but only one format field. A format field has the following form:



%[flag] [width] [.precision] type

[flag]: The flag values perform the following (optional):
Flag Meaning Default
-
Left align the result within the given field width. Right align.
+
Prefix the output value with a sign (+ or –). Sign appears only for negative signed values (–).
0
If width is prefixed with 0, zeros are added until the minimum width is reached. If 0 and – appear, the 0 is ignored. No padding.
blank
Prefix the output value with a blank if the output value is signed and positive; the blank is ignored if both the blank and + flags appear. No blank appears.
#


When used with the o, x, or X format, the # flag prefixes any nonzero output value with 0, 0x, or 0X, respectively.



When used with the e, E, or f format, the # flag forces the output value to contain a decimal point in all cases.



When used with the g or G format, the # flag forces the output value to contain a decimal point in all cases and prevents the truncation of trailing zeros.
Ignored when used with c, d, i, u, or s.



No prefix appears (o, x, X).



Decimal point appears only if digits follow it (e, E, f).



Decimal point appears only if digits follow it. Trailing zeros are truncated. (g or G)


[width]
: The minimum number of characters to display. If the number contains less than the mimimum number of characters, the remainder will be displayed as described by the [flags] parameter. (Optional)

[.precision]: The minimum number of characters to display after the decimal point for a 'real' input. This is subtracted from the total 'width', including the decimal point. (Optional)

type: The type parameter describes how to treat the input (integer or real). This parameter is required. The type is interpreted according to the following table:


Type Input Output Format
c
int Treats the input as a 256 bit ASCII character.
d
int Treats the input as a signed decimal integer
i
int Treats the input as a signed decimal integer
o
int Treats the input as an unsigned octal integer
u


int



Treats the input as an unsigned decimal integer

x
int Treats the input as an unsigned hexadecimal integer (lowercase)
X
int Treats the input as an unsigned hexadecimal integer (uppercase)
e
real Treats the input as a signed number with an exponent (ex: ddd.dd e dd)
E
real Treats the input as a signed number with an exponent (ex: ddd.dd E dd)
f
real Treats the input as a signed floating point number (ex: ddd.ddd)
g
real Chooses either e or f format for input, whichever is more compact.
G
real Chooses either E or f format for input, whichever is more compact.


For example, to format an integer to show 5 characters with leading zeros and a signed value, [flag] is 0, [width] is 5, and type is d. The full format is therefore "%05d".


Example Code
1 >>int i = 42;
2 >>disp(format("%05d", i));
00042


To format a real as an with an exponent with 6 total characters, two of which are after the decimal point, with blanks as the leading spaces, [flag] is ' ' (blank), [width] is 6, [.precision] is .2, and type is e. The full format is therefore "% 6.2e".


Example Code
1 >>real r = 623589.42;
2 >>disp(format("% 6.2e", r));
 6.24e+005


The following is some sample code displaying some of the uses of the format command:


Example Code
# This script displays the use of the format() function

# Sample integers
int i1 = 72;
int i2 = -38;


# Sample reals
real r1 = 0.000046;
real r2 = -60894.5;


# Output string
string s;


# Formats for i1
disp("---- Displaying Formats for i1 ----");
disp(i1); # Nominal format

# Display leading 0s and sign, 6 characters total
s = format("Sign and leading 0s: %+06d", i1);
disp(s);


# Display as ASCII character
s = format("ASCII character: %c", i1);
disp(s);


# Display as octal value
s = format("Octal value: %o", i1);
disp(s);


# Display as hex value with leading 0x
s = format("Hex value: %#x", i1);
disp(s);


# Formats for i2
disp("\n---- Displaying Formats for i2 ----");
disp(i2); # Nominal format


# Display leading 0s and sign, 6 characters total
s = format("Sign and leading 0s: %+06d", i2);
disp(s);


# Display unsigned value
s = format("Unsigned equivalent: %u", i2);
disp(s);


# Display as octal value
s = format("Octal value: %o", i2);
disp(s);


# Display as hex value with leading 0x
s = format("Hex value: %#x", i2);
disp(s);


# Formats for r1
disp("\n---- Displaying Formats for r1 ----");
disp(r1); # Nominal format


# Display leading 0s and sign, 12 characters total
s = format("Sign and leading 0s: %+014f", r1);
disp(s);


# Display exponent with 3 decimal places
s = format("With exponent, 2 decimal places: %6.2e", r1);
disp(s);


# Display exponent with 3 decimal places
s = format("Choose f or e: %6g", r1);
disp(s);


# Formats for r2
disp("\n---- Displaying Formats for r2 ----");
disp(r2); # Nominal format


# Display leading 0s and sign, 12 characters total
s = format("Sign and leading 0s: %+014f", r2);
disp(s);


# Display exponent with 3 decimal places
s = format("With exponent, 2 decimal places: %6.2e", r2);
disp(s);


# Display exponent with 3 decimal places
s = format("Choose f or e: %6g", r2);
disp(s);


The output from the above sample is as follows:


Output
---- Displaying Formats for i1 ----
72
Sign and leading 0s: +00072
ASCII character: H
Octal value: 110
Hex value: 0x48


---- Displaying Formats for i2 ----
-38
Sign and leading 0s: -00038
Unsigned equivalent: 4294967258
Octal value: 37777777732
Hex value: 0xffffffda


---- Displaying Formats for r1 ----
0.00004600000000
Sign and leading 0s: +000000.000046
With exponent, 2 decimal places: 4.60e-005
Choose f or e: 4.6e-005


---- Displaying Formats for r2 ----
-60894.50000000000000
Sign and leading 0s: -060894.500000
With exponent, 2 decimal places: -6.09e+004
Choose f or e: -60894.5




Copyright 2000-2011 Phantom Automated Solutions