]> gitweb.factorcode.org Git - factor.git/blob - extra/printf/printf-docs.factor
Merge branch 'master' into experimental (untested!)
[factor.git] / extra / printf / printf-docs.factor
1
2 USING: help.syntax help.markup kernel prettyprint sequences strings ;
3
4 IN: printf
5
6 HELP: printf
7 { $values { "format-string" string } }
8 { $description "Writes the arguments (specified on the stack) formatted according to the format string." } 
9 { $examples 
10     { $example
11         "USING: printf ;"
12         "123 \"%05d\" printf"
13         "00123" }
14     { $example
15         "USING: printf ;"
16         "HEX: ff \"%04X\" printf"
17         "00FF" }
18     { $example
19         "USING: printf ;"
20         "1.23456789 \"%.3f\" printf"
21         "1.235" }
22     { $example 
23         "USING: printf ;"
24         "1234567890 \"%.5e\" printf"
25         "1.23457e+09" }
26     { $example
27         "USING: printf ;"
28         "12 \"%'#4d\" printf"
29         "##12" }
30     { $example
31         "USING: printf ;"
32         "1234 \"%+d\" printf"
33         "+1234" }
34 } ;
35
36 HELP: sprintf
37 { $values { "format-string" string } { "result" string } }
38 { $description "Returns the arguments (specified on the stack) formatted according to the format string as a result string." } 
39 { $see-also printf } ;
40
41 ARTICLE: "printf" "Formatted printing"
42 "The " { $vocab-link "printf" } " vocabulary is used for formatted printing.\n"
43 { $subsection printf }
44 { $subsection sprintf }
45 "\n"
46 "Several format specifications exist for handling arguments of different types, and specifying attributes for the result string, including such things as maximum width, padding, and decimals.\n"
47 { $table
48     { "%%"    "Single %" "" }
49     { "%P.Ds" "String format" "string" }
50     { "%P.DS" "String format uppercase" "string" }
51     { "%c"    "Character format" "char" } 
52     { "%C"    "Character format uppercase" "char" } 
53     { "%+Pd"   "Integer format"  "fixnum" }
54     { "%+P.De" "Scientific notation" "fixnum, float" }
55     { "%+P.DE" "Scientific notation" "fixnum, float" }
56     { "%+P.Df" "Fixed format" "fixnum, float" }
57     { "%+Px"   "Hexadecimal" "hex" }
58     { "%+PX"   "Hexadecimal uppercase" "hex" }
59 }
60 "\n"
61 "A plus sign ('+') is used to optionally specify that the number should be formatted with a '+' preceeding it if positive.\n"
62 "\n"
63 "Padding ('P') is used to optionally specify the minimum width of the result string, the padding character, and the alignment.  By default, the padding character defaults to a space and the alignment defaults to right-aligned. For example:\n"
64 { $list
65     "\"%5s\" formats a string padding with spaces up to 5 characters wide."
66     "\"%08d\" formats an integer padding with zeros up to 3 characters wide."
67     "\"%'#5f\" formats a float padding with '#' up to 3 characters wide."
68     "\"%-10d\" formats an integer to 10 characters wide and left-aligns." 
69 }
70 "\n"
71 "Digits ('D') is used to optionally specify the maximum digits in the result string. For example:\n"
72 { $list 
73     "\"%.3s\" formats a string to truncate at 3 characters (from the left)."
74     "\"%.10f\" formats a float to pad-right with zeros up to 10 digits beyond the decimal point."
75     "\"%.5E\" formats a float into scientific notation with zeros up to 5 digits beyond the decimal point, but before the exponent."
76 } ;
77
78 ABOUT: "printf"
79
80