Sprintf Flags and Field Types : sprintf « Development « Ruby






Sprintf Flags and Field Types


Flag    For field types         Description
[space] bdeEfgGiouxX            Places a space at the start of a positive number.
[1-9]$  All field types         Absolute number of an argument for this field.
#       beEfgGoxX               For the field b, prefixes the result with 0b; for o, with 0; for x, with 0x; for X, with 0X. For e, E, f, g, and G, adds decimal point. For g and G, does not remove trailing spaces.
+       bdeEfgGiouxX            Adds a leading plus sign (+) to positive numbers.
-       All field types         Left-justifies the result.
0       bdeEfgGiouxX            Pads the result with zeros (0) instead of spaces.
*       All field types         Uses the next argument as the field width. If negative, left-justifies the result. If asterisk (*) is followed by a number and a dollar sign ($), uses argument as width.

 








Related examples in the same category

1.Field types for sprintf
2.Use sprintf("%08b", num) to convert input to binary number
3.Print out time in Begin and end block
4.sprintf( "%0.2f", my_bmi )