Ruby/Development/sprintf

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

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.



Print out time in Begin and end block

#!/usr/bin/env ruby
BEGIN { puts "Date and time: " + Time.now.to_s }
def bmi( weight, height )
  703.0*( weight.to_f/(height.to_f**2))
end
my_bmi = bmi( 196, 73 )
puts "Your BMI is: " + x = sprintf( "%0.2f", my_bmi )
END { puts "You"ve got some work ahead of you." }



sprintf( "%0.2f", my_bmi )

#!/usr/bin/env ruby
BEGIN { puts "Date and time: " + Time.now.to_s }
def bmi( weight, height )
  703.0*( weight.to_f/(height.to_f**2))
end
my_bmi = bmi( 196, 73 )
puts "Your BMI is: " + x = sprintf( "%0.2f", my_bmi )



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.



Use sprintf("%08b", num) to convert input to binary number

#!/usr/bin/env ruby
module Binary
  def Binary.to_bin( num )
    bin = sprintf("%08b", num)
  end
end
p Binary.to_bin( 123 ) # => "01111011"