summaryrefslogtreecommitdiff
path: root/doc/format_specifications.rdoc
blob: e589524f279365a88e7fddd0c57f02f92f65361a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
== Format Specifications

Several Ruby core classes have instance method +printf+ or +sprintf+:

- ARGF#printf
- IO#printf
- Kernel#printf
- Kernel#sprintf

Each of these methods takes:

- Argument +format_string+, which has zero or more
  embedded _format_ _specifications_ (see below).
- Arguments <tt>*arguments</tt>, which are zero or more objects to be formatted.

Each of these methods prints or returns the string
resulting from replacing each
format specification embedded in +format_string+ with a string form
of the corresponding argument among +arguments+.

A simple example:

  sprintf('Name: %s; value: %d', 'Foo', 0) # => "Name: Foo; value: 0"

A format specification has the form:

  %[flags][width][.precision]type

It consists of:

- A leading percent character.
- Zero or more _flags_ (each is a character).
- An optional _width_ _specifier_ (an integer).
- An optional _precision_ _specifier_ (a period followed by a non-negative integer).
- A _type_ _specifier_ (a character).

Except for the leading percent character,
the only required part is the type specifier, so we begin with that.

=== Type Specifiers

This section provides a brief explanation of each type specifier.
The links lead to the details and examples.

==== \Integer Type Specifiers

- +b+ or +B+: Format +argument+ as a binary integer.
  See {Specifiers b and B}[rdoc-ref:format_specifications.rdoc@Specifiers+b+and+B].
- +d+, +i+, or +u+ (all are identical):
  Format +argument+ as a decimal integer.
  See {Specifier d}[rdoc-ref:format_specifications.rdoc@Specifier+d].
- +o+: Format +argument+ as an octal integer.
  See {Specifier o}[rdoc-ref:format_specifications.rdoc@Specifier+o].
- +x+ or +X+: Format +argument+ as a hexadecimal integer.
  See {Specifiers x and X}[rdoc-ref:format_specifications.rdoc@Specifiers+x+and+X].

==== Floating-Point Type Specifiers

- +a+ or +A+: Format +argument+ as hexadecimal floating-point number.
  See {Specifiers a and A}[rdoc-ref:format_specifications.rdoc@Specifiers+a+and+A].
- +e+ or +E+: Format +argument+ in scientific notation.
  See {Specifiers e and E}[rdoc-ref:format_specifications.rdoc@Specifiers+e+and+E].
- +f+: Format +argument+ as a decimal floating-point number.
  See {Specifier f}[rdoc-ref:format_specifications.rdoc@Specifier+f].
- +g+ or +G+: Format +argument+ in a "general" format.
  See {Specifiers g and G}[rdoc-ref:format_specifications.rdoc@Specifiers+g+and+G].

==== Other Type Specifiers

- +c+: Format +argument+ as a character.
  See {Specifier c}[rdoc-ref:format_specifications.rdoc@Specifier+c].
- +p+: Format +argument+ as a string via <tt>argument.inspect</tt>.
  See {Specifier p}[rdoc-ref:format_specifications.rdoc@Specifier+p].
- +s+: Format +argument+ as a string via <tt>argument.to_s</tt>.
  See {Specifier s}[rdoc-ref:format_specifications.rdoc@Specifier+s].
- <tt>%</tt>: Format +argument+ (<tt>'%'</tt>) as a single percent character.
  See {Specifier %}[rdoc-ref:format_specifications.rdoc@Specifier+-25].

=== Flags

The effect of a flag may vary greatly among type specifiers.
These remarks are general in nature.
See {type-specific details}[rdoc-ref:format_specifications.rdoc@Type+Specifier+Details+and+Examples].

Multiple flags may be given with single type specifier;
order does not matter.

==== <tt>' '</tt> Flag

Insert a space before a non-negative number:

  sprintf('%d', 10)  # => "10"
  sprintf('% d', 10) # => " 10"

Insert a minus sign for negative value:

  sprintf('%d', -10)  # => "-10"
  sprintf('% d', -10) # => "-10"

==== <tt>'#'</tt> Flag

Use an alternate format; varies among types:

  sprintf('%x', 100)  # => "64"
  sprintf('%#x', 100) # => "0x64"

==== <tt>'+'</tt> Flag

Add a leading plus sign for a non-negative number:

  sprintf('%x', 100)  # => "64"
  sprintf('%+x', 100) # => "+64"

==== <tt>'-'</tt> Flag

Left justify the value in its field:

  sprintf('%6d', 100)  # => "   100"
  sprintf('%-6d', 100) # => "100   "

==== <tt>'0'</tt> Flag

Left-pad with zeros instead of spaces:

  sprintf('%6d', 100)  # => "   100"
  sprintf('%06d', 100) # => "000100"

==== <tt>'*'</tt> Flag

Use the next argument as the field width:

  sprintf('%d', 20, 14)  # => "20"
  sprintf('%*d', 20, 14) # => "                  14"

==== <tt>'n$'</tt> Flag

Format the (1-based) <tt>n</tt>th argument into this field:

    sprintf("%s %s", 'world', 'hello')     # => "world hello"
    sprintf("%2$s %1$s", 'world', 'hello') # => "hello world"

=== Width Specifier

In general, a width specifier determines the minimum width (in characters)
of the formatted field:

  sprintf('%10d', 100)  # => "       100"

  # Left-justify if negative.
  sprintf('%-10d', 100) # => "100       "

  # Ignore if too small.
  sprintf('%1d', 100)   # => "100"

=== Precision Specifier

A precision specifier is a decimal point followed by zero or more
decimal digits.

For integer type specifiers, the precision specifies the minimum number of
digits to be written. If the precision is shorter than the integer, the result is
padded with leading zeros. There is no modification or truncation of the result
if the integer is longer than the precision:

  sprintf('%.3d', 1)    # => "001"
  sprintf('%.3d', 1000) # => "1000"

  # If the precision is 0 and the value is 0, nothing is written
  sprintf('%.d', 0)  # => ""
  sprintf('%.0d', 0) # => ""

For the +a+/+A+, +e+/+E+, +f+/+F+ specifiers, the precision specifies
the number of digits after the decimal point to be written:

  sprintf('%.2f', 3.14159)  # => "3.14"
  sprintf('%.10f', 3.14159) # => "3.1415900000"

  # With no precision specifier, defaults to 6-digit precision.
  sprintf('%f', 3.14159)    # => "3.141590"

For the +g+/+G+ specifiers, the precision specifies
the number of significant digits to be written:

  sprintf('%.2g', 123.45)  # => "1.2e+02"
  sprintf('%.3g', 123.45)  # => "123"
  sprintf('%.10g', 123.45) # =>  "123.45"

  # With no precision specifier, defaults to 6 significant digits.
  sprintf('%g', 123.456789) # => "123.457"

For the +s+, +p+ specifiers, the precision specifies
the number of characters to write:

  sprintf('%s', Time.now)    # => "2022-05-04 11:59:16 -0400"
  sprintf('%.10s', Time.now) # => "2022-05-04"

=== Type Specifier Details and Examples

==== Specifiers +a+ and +A+

Format +argument+ as hexadecimal floating-point number:

  sprintf('%a', 3.14159)   # => "0x1.921f9f01b866ep+1"
  sprintf('%a', -3.14159)  # => "-0x1.921f9f01b866ep+1"
  sprintf('%a', 4096)      # => "0x1p+12"
  sprintf('%a', -4096)     # => "-0x1p+12"

  # Capital 'A' means that alphabetical characters are printed in upper case.
  sprintf('%A', 4096)      # => "0X1P+12"
  sprintf('%A', -4096)     # => "-0X1P+12"

==== Specifiers +b+ and +B+

The two specifiers +b+ and +B+ behave identically
except when flag <tt>'#'</tt>+ is used.

Format +argument+ as a binary integer:

  sprintf('%b', 1)  # => "1"
  sprintf('%b', 4)  # => "100"

  # Prefix '..' for negative value.
  sprintf('%b', -4) # => "..100"

  # Alternate format.
  sprintf('%#b', 4)  # => "0b100"
  sprintf('%#B', 4)  # => "0B100"

==== Specifier +c+

Format +argument+ as a single character:

  sprintf('%c', 'A') # => "A"
  sprintf('%c', 65)  # => "A"

==== Specifier +d+

Format +argument+ as a decimal integer:

  sprintf('%d', 100)  # => "100"
  sprintf('%d', -100) # => "-100"

Flag <tt>'#'</tt> does not apply.

==== Specifiers +e+ and +E+

Format +argument+ in
{scientific notation}[https://en.wikipedia.org/wiki/Scientific_notation]:

  sprintf('%e', 3.14159)  # => "3.141590e+00"
  sprintf('%E', -3.14159) # => "-3.141590E+00"

==== Specifier +f+

Format +argument+ as a floating-point number:

  sprintf('%f', 3.14159)  # => "3.141590"
  sprintf('%f', -3.14159) # => "-3.141590"

Flag <tt>'#'</tt> does not apply.

==== Specifiers +g+ and +G+

Format +argument+ using exponential form (+e+/+E+ specifier)
if the exponent is less than -4 or greater than or equal to the precision.
Otherwise format +argument+ using floating-point form (+f+ specifier):

  sprintf('%g', 100)  # => "100"
  sprintf('%g', 100.0)  # => "100"
  sprintf('%g', 3.14159)  # => "3.14159"
  sprintf('%g', 100000000000)  # => "1e+11"
  sprintf('%g', 0.000000000001)  # => "1e-12"

  # Capital 'G' means use capital 'E'.
  sprintf('%G', 100000000000)  # => "1E+11"
  sprintf('%G', 0.000000000001)  # => "1E-12"

  # Alternate format.
  sprintf('%#g', 100000000000)  # => "1.00000e+11"
  sprintf('%#g', 0.000000000001)  # => "1.00000e-12"
  sprintf('%#G', 100000000000)  # => "1.00000E+11"
  sprintf('%#G', 0.000000000001)  # => "1.00000E-12"

==== Specifier +o+

Format +argument+ as an octal integer.
If +argument+ is negative, it will be formatted as a two's complement
prefixed with +..7+:

  sprintf('%o', 16)   # => "20"

  # Prefix '..7' for negative value.
  sprintf('%o', -16)  # => "..760"

  # Prefix zero for alternate format if positive.
  sprintf('%#o', 16)  # => "020"
  sprintf('%#o', -16) # => "..760"

==== Specifier +p+

Format +argument+ as a string via <tt>argument.inspect</tt>:

  t = Time.now
  sprintf('%p', t)   # => "2022-05-01 13:42:07.1645683 -0500"

==== Specifier +s+

Format +argument+ as a string via <tt>argument.to_s</tt>:

  t = Time.now
  sprintf('%s', t) # => "2022-05-01 13:42:07 -0500"

Flag <tt>'#'</tt> does not apply.

==== Specifiers +x+ and +X+

Format +argument+ as a hexadecimal integer.
If +argument+ is negative, it will be formatted as a two's complement
prefixed with +..f+:

  sprintf('%x', 100)   # => "64"

  # Prefix '..f' for negative value.
  sprintf('%x', -100)  # => "..f9c"

  # Use alternate format.
  sprintf('%#x', 100)  # => "0x64"

  # Alternate format for negative value.
  sprintf('%#x', -100) # => "0x..f9c"

==== Specifier <tt>%</tt>

Format +argument+ (<tt>'%'</tt>) as a single percent character:

  sprintf('%d %%', 100) # => "100 %"

Flags do not apply.

=== Reference by Name

For more complex formatting, Ruby supports a reference by name.
%<name>s style uses format style, but %{name} style doesn't.

Examples:

  sprintf("%<foo>d : %<bar>f", { :foo => 1, :bar => 2 }) # => 1 : 2.000000
  sprintf("%{foo}f", { :foo => 1 })                      # => "1f"