]> gitweb.factorcode.org Git - factor.git/blob - basis/formatting/formatting-docs.factor
Fix conflict
[factor.git] / basis / formatting / formatting-docs.factor
1
2 USING: help.syntax help.markup kernel prettyprint sequences strings ;
3
4 IN: formatting
5
6 HELP: printf
7 { $values { "format-string" string } }
8 { $description 
9     "Writes the arguments (specified on the stack) formatted according to the format string.\n" 
10     $nl
11     "Several format specifications exist for handling arguments of different types, and "
12     "specifying attributes for the result string, including such things as maximum width, "
13     "padding, and decimals.\n"
14     { $table
15         { "%%"      "Single %"                   "" }
16         { "%P.Ds"   "String format"              "string" }
17         { "%P.DS"   "String format uppercase"    "string" }
18         { "%c"      "Character format"           "char" } 
19         { "%C"      "Character format uppercase" "char" } 
20         { "%+Pd"    "Integer format"             "fixnum" }
21         { "%+P.De"  "Scientific notation"        "fixnum, float" }
22         { "%+P.DE"  "Scientific notation"        "fixnum, float" }
23         { "%+P.Df"  "Fixed format"               "fixnum, float" }
24         { "%+Px"    "Hexadecimal"                "hex" }
25         { "%+PX"    "Hexadecimal uppercase"      "hex" }
26     }
27     $nl
28     "A plus sign ('+') is used to optionally specify that the number should be "
29     "formatted with a '+' preceeding it if positive.\n"
30     $nl
31     "Padding ('P') is used to optionally specify the minimum width of the result "
32     "string, the padding character, and the alignment.  By default, the padding "
33     "character defaults to a space and the alignment defaults to right-aligned. "
34     "For example:\n"
35     { $list
36         "\"%5s\" formats a string padding with spaces up to 5 characters wide."
37         "\"%08d\" formats an integer padding with zeros up to 3 characters wide."
38         "\"%'#5f\" formats a float padding with '#' up to 3 characters wide."
39         "\"%-10d\" formats an integer to 10 characters wide and left-aligns." 
40     }
41     $nl
42     "Digits ('D') is used to optionally specify the maximum digits in the result "
43     "string. For example:\n"
44     { $list 
45         "\"%.3s\" formats a string to truncate at 3 characters (from the left)."
46         "\"%.10f\" formats a float to pad-right with zeros up to 10 digits beyond the decimal point."
47         "\"%.5E\" formats a float into scientific notation with zeros up to 5 digits beyond the decimal point, but before the exponent."
48     }
49 }
50 { $examples 
51     { $example
52         "USING: formatting ;"
53         "123 \"%05d\" printf"
54         "00123" }
55     { $example
56         "USING: formatting ;"
57         "HEX: ff \"%04X\" printf"
58         "00FF" }
59     { $example
60         "USING: formatting ;"
61         "1.23456789 \"%.3f\" printf"
62         "1.235" }
63     { $example 
64         "USING: formatting ;"
65         "1234567890 \"%.5e\" printf"
66         "1.23457e+09" }
67     { $example
68         "USING: formatting ;"
69         "12 \"%'#4d\" printf"
70         "##12" }
71     { $example
72         "USING: formatting ;"
73         "1234 \"%+d\" printf"
74         "+1234" }
75 } ;
76
77 HELP: sprintf
78 { $values { "format-string" string } { "result" string } }
79 { $description "Returns the arguments (specified on the stack) formatted according to the format string as a result string." } 
80 { $see-also printf } ;
81
82 HELP: strftime
83 { $values { "format-string" string } }
84 { $description 
85     "Writes the timestamp (specified on the stack) formatted according to the format string.\n"
86     $nl
87     "Different attributes of the timestamp can be retrieved using format specifications.\n"
88     { $table
89         { "%a"     "Abbreviated weekday name." }
90         { "%A"     "Full weekday name." }
91         { "%b"     "Abbreviated month name." }
92         { "%B"     "Full month name." }
93         { "%c"     "Date and time representation." }
94         { "%d"     "Day of the month as a decimal number [01,31]." }
95         { "%H"     "Hour (24-hour clock) as a decimal number [00,23]." }
96         { "%I"     "Hour (12-hour clock) as a decimal number [01,12]." }
97         { "%j"     "Day of the year as a decimal number [001,366]." }
98         { "%m"     "Month as a decimal number [01,12]." }
99         { "%M"     "Minute as a decimal number [00,59]." }
100         { "%p"     "Either AM or PM." }
101         { "%S"     "Second as a decimal number [00,59]." }
102         { "%U"     "Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]." }
103         { "%w"     "Weekday as a decimal number [0(Sunday),6]." }
104         { "%W"     "Week number of the year (Monday as the first day of the week) as a decimal number [00,53]." }
105         { "%x"     "Date representation." }
106         { "%X"     "Time representation." }
107         { "%y"     "Year without century as a decimal number [00,99]." }
108         { "%Y"     "Year with century as a decimal number." }
109         { "%Z"     "Time zone name (no characters if no time zone exists)." }
110         { "%%"     "A literal '%' character." }
111     } 
112
113 { $examples 
114     { $unchecked-example
115         "USING: calendar formatting io ;"
116         "now \"%c\" strftime print"
117         "Mon Dec 15 14:40:43 2008" }
118 } ;
119
120 ARTICLE: "formatting" "Formatted printing"
121 "The " { $vocab-link "formatting" } " vocabulary is used for formatted printing."
122 { $subsection printf }
123 { $subsection sprintf }
124 { $subsection strftime }
125 ;
126
127 ABOUT: "formatting"
128
129