Field types for sprintf : sprintf « Development « Ruby






Field types for sprintf


Field   Description
b       Converts a numeric argument to binary.
c       Converts a numeric argument (character code) to a character.
d       Converts a numeric argument to a decimal number. 
        Same as i.
e       Converts a floating-point argument into exponential notation using one digit before the decimal point. 
        Defaults to six fractional digits. 
E       Same as e, but uses E in result.
f       Converts a numeric argument to a floating-point number. 
        Defaults to six fractional digits. 
        Precision determines the number of fractional digits.
g       Converts a numeric argument to a floating point number using the exponential form 
G       Same as g, 
        Uses E in result.
i       Converts a numeric argument to a decimal number.
o       Converts a numeric argument to octal.
p       Outputs a printable version of the argument, with special characters escaped.
s       Substitutes an argument as a string. 
u       Treats argument as an unsigned decimal. 
        Negative integers are displayed as a 32-bit two's complement plus one for the underlying architecture (for example, 2**32+n). 
x       Converts a numeric argument to hexadecimal with lowercase letters a through f. 
        Negative numbers are displayed with two leading periods, indicating an infinite string of leading ffs.
X       Same as x, but uses uppercase letters A through F in the result. 
        Negative numbers are displayed with two leading periods, indicating an infinite string of leading FFs.

 








Related examples in the same category

1.Sprintf Flags and Field Types
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 )