Several Ruby core classes have instance method printf
or sprintf
:
Each of these methods takes:
Argument format_string
, which has zero or more embedded format specifications (see below).
Arguments *arguments
, 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)
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.
d
, i
, or u
(all are identical): Format argument
as a decimal integer. See Specifier d.
o
: Format argument
as an octal integer. See Specifier o.
x
or X
: Format argument
as a hexadecimal integer. See Specifiers x and X.
a
or A
: Format argument
as hexadecimal floating-point number. See Specifiers a and A.
e
or E
: Format argument
in scientific notation. See Specifiers e and E.
f
: Format argument
as a decimal floating-point number. See Specifier f.
g
or G
: Format argument
in a âgeneralâ format. See Specifiers g and G.
c
: Format argument
as a character. See Specifier c.
p
: Format argument
as a string via argument.inspect
. See Specifier p.
s
: Format argument
as a string via argument.to_s
. See Specifier s.
%
: Format argument
('%'
) as a single percent character. See Specifier %.
The effect of a flag may vary greatly among type specifiers. These remarks are general in nature. See type-specific details.
Multiple flags may be given with single type specifier; order does not matter.
' '
Flag¶ ↑
Insert a space before a non-negative number:
sprintf('%d', 10) sprintf('% d', 10)
Insert a minus sign for negative value:
sprintf('%d', -10) sprintf('% d', -10)
'#'
Flag¶ ↑
Use an alternate format; varies among types:
sprintf('%x', 100) sprintf('%#x', 100)
'+'
Flag¶ ↑
Add a leading plus sign for a non-negative number:
sprintf('%x', 100) sprintf('%+x', 100)
'-'
Flag¶ ↑
Left justify the value in its field:
sprintf('%6d', 100) sprintf('%-6d', 100)
'0'
Flag¶ ↑
Left-pad with zeros instead of spaces:
sprintf('%6d', 100) sprintf('%06d', 100)
'*'
Flag¶ ↑
Use the next argument as the field width:
sprintf('%d', 20, 14) sprintf('%*d', 20, 14)
'n$'
Flag¶ ↑
Format the (1-based) n
th argument into this field:
sprintf("%s %s", 'world', 'hello') sprintf("%2$s %1$s", 'world', 'hello')Width Specifier¶ ↑
In general, a width specifier determines the minimum width (in characters) of the formatted field:
sprintf('%10d', 100) sprintf('%-10d', 100) sprintf('%1d', 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) sprintf('%.3d', 1000) 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) sprintf('%.10f', 3.14159) sprintf('%f', 3.14159)
For the g
/G
specifiers, the precision specifies the number of significant digits to be written:
sprintf('%.2g', 123.45) sprintf('%.3g', 123.45) sprintf('%.10g', 123.45) sprintf('%g', 123.456789)
For the s
, p
specifiers, the precision specifies the number of characters to write:
sprintf('%s', Time.now) sprintf('%.10s', Time.now)Type Specifier Details and Examples¶ ↑ Specifiers
a
and A
¶ ↑
Format argument
as hexadecimal floating-point number:
sprintf('%a', 3.14159) sprintf('%a', -3.14159) sprintf('%a', 4096) sprintf('%a', -4096) sprintf('%A', 4096) sprintf('%A', -4096)Specifiers
b
and B
¶ ↑
The two specifiers b
and B
behave identically except when flag '#'
+ is used.
Format argument
as a binary integer:
sprintf('%b', 1) sprintf('%b', 4) sprintf('%b', -4) sprintf('%#b', 4) sprintf('%#B', 4)Specifier
c
¶ ↑
Format argument
as a single character:
sprintf('%c', 'A') sprintf('%c', 65)
This behaves like String#<<
, except for raising ArgumentError
instead of RangeError
.
d
¶ ↑
Format argument
as a decimal integer:
sprintf('%d', 100) sprintf('%d', -100)
Flag '#'
does not apply.
e
and E
¶ ↑
Format argument
in scientific notation:
sprintf('%e', 3.14159) sprintf('%E', -3.14159)Specifier
f
¶ ↑
Format argument
as a floating-point number:
sprintf('%f', 3.14159) sprintf('%f', -3.14159)
Flag '#'
does not apply.
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) sprintf('%g', 100.0) sprintf('%g', 3.14159) sprintf('%g', 100000000000) sprintf('%g', 0.000000000001) sprintf('%G', 100000000000) sprintf('%G', 0.000000000001) sprintf('%#g', 100000000000) sprintf('%#g', 0.000000000001) sprintf('%#G', 100000000000) sprintf('%#G', 0.000000000001)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) sprintf('%o', -16) sprintf('%#o', 16) sprintf('%#o', -16)Specifier
p
¶ ↑
Format argument
as a string via argument.inspect
:
t = Time.now sprintf('%p', t)Specifier
s
¶ ↑
Format argument
as a string via argument.to_s
:
t = Time.now sprintf('%s', t)
Flag '#'
does not apply.
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) sprintf('%x', -100) sprintf('%#x', 100) sprintf('%#x', -100)Specifier
%
¶ ↑
Format argument
('%'
) as a single percent character:
sprintf('%d %%', 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 }) sprintf("%{foo}f", { :foo => 1 })
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4