sprintf
sprintf(...)
public
Returns the string resulting from applying format_string to any additional arguments. Within the format string, any characters other than format sequences are copied to the result. A format sequence consists of a percent sign, followed by optional flags, width, and precision indicators, then terminated with a field type character. The field type controls how the corresponding sprintf argument is to be interpreted, while the flags modify that interpretation. The field type characters are listed in the table at the end of this section. The flag characters are:
Flag | Applies to | Meaning ---------+--------------+----------------------------------------- space | bdeEfgGiouxX | Leave a space at the start of | | positive numbers. ---------+--------------+----------------------------------------- (digit)$ | all | Specifies the absolute argument number | | for this field. Absolute and relative | | argument numbers cannot be mixed in a | | sprintf string. ---------+--------------+----------------------------------------- # | beEfgGoxX | Use an alternative format. For the | | conversions `o', `x', `X', and `b', | | prefix the result with ``0'', ``0x'', ``0X'', | | and ``0b'', respectively. For `e', | | `E', `f', `g', and 'G', force a decimal | | point to be added, even if no digits follow. | | For `g' and 'G', do not remove trailing zeros. ---------+--------------+----------------------------------------- + | bdeEfgGiouxX | Add a leading plus sign to positive numbers. ---------+--------------+----------------------------------------- - | all | Left-justify the result of this conversion. ---------+--------------+----------------------------------------- 0 (zero) | bdeEfgGiouxX | Pad with zeros, not spaces. ---------+--------------+----------------------------------------- * | all | Use the next argument as the field width. | | If negative, left-justify the result. If the | | asterisk is followed by a number and a dollar | | sign, use the indicated argument as the width.
The field width is an optional integer, followed optionally by a period and a precision. The width specifies the minimum number of characters that will be written to the result for this field. For numeric fields, the precision controls the number of decimal places displayed. For string fields, the precision determines the maximum number of characters to be copied from the string. (Thus, the format sequence %10.10s will always contribute exactly ten characters to the result.)
The field types are:
Field | Conversion ------+-------------------------------------------------------------- b | Convert argument as a binary number. c | Argument is the numeric code for a single character. d | Convert argument as a decimal number. E | Equivalent to `e', but uses an uppercase E to indicate | the exponent. e | Convert floating point argument into exponential notation | with one digit before the decimal point. The precision | determines the number of fractional digits (defaulting to six). f | Convert floating point argument as [-]ddd.ddd, | where the precision determines the number of digits after | the decimal point. G | Equivalent to `g', but use an uppercase `E' in exponent form. g | Convert a floating point number using exponential form | if the exponent is less than -4 or greater than or | equal to the precision, or in d.dddd form otherwise. i | Identical to `d'. o | Convert argument as an octal number. p | The valuing of argument.inspect. s | Argument is a string to be substituted. If the format | sequence contains a precision, at most that many characters | will be copied. u | Treat argument as an unsigned decimal number. Negative integers | are displayed as a 32 bit two's complement plus one for the | underlying architecture; that is, 2 ** 32 + n. However, since | Ruby has no inherent limit on bits used to represent the | integer, this value is preceded by two dots (..) in order to | indicate a infinite number of leading sign bits. X | Convert argument as a hexadecimal number using uppercase | letters. Negative numbers will be displayed with two | leading periods (representing an infinite string of | leading 'FF's. x | Convert argument as a hexadecimal number. | Negative numbers will be displayed with two | leading periods (representing an infinite string of | leading 'ff's.
Examples:
sprintf("%d %04x", 123, 123) #=> "123 007b" sprintf("%08b '%4s'", 123, 123) #=> "01111011 ' 123'" sprintf("%1$*2$s %2$d %1$s", "hello", 8) #=> " hello 8 hello" sprintf("%1$*2$s %2$d", "hello", -8) #=> "hello -8" sprintf("%+g:% g:%-g", 1.23, 1.23, 1.23) #=> "+1.23: 1.23:1.23" sprintf("%u", -123) #=> "..4294967173"
Shortcut
According to http://henrik.nyh.se/2008/01/surround-helper-alternative-in-haml there is a short version for sprintf:
“(%s)” % “foo” is the same as sprintf(“(%s)”, “foo”)
Can someone who knows write more about this here? How do I work with multiple strings? Is this even possible? “(%s %t)” % “foo”, “bar” does not work.
Unexpected rounding behavior
Both 2.5 and 1.5 are rounded to 2…
ree-1.8.7-2010.02 > sprintf("%.f", 0.5) => "0" ree-1.8.7-2010.02 > sprintf("%.f", 1.5) => "2" ree-1.8.7-2010.02 > sprintf("%.f", 2.5) => "2" ree-1.8.7-2010.02 > sprintf("%.f", 3.5) => "4" ree-1.8.7-2010.02 > sprintf("%.f", 4.5) => "4"
use round instead to get proper behavior
Shortcut with %
Thanks iamcata, that works :). And I finally found the right place to put this comment: http://apidock.com/ruby/String/%25#726-Use-it-with-HAML
Shortcut with %
@tordans for multiple args wrap the args in an array