Ruby/Number/Number Literal

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

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

Содержание

Add commas to a big integer value

def commas(x)
  str = x.to_s.reverse
  str.gsub!("([0-9]{3})","\\1,")
  str.gsub(",$","").reverse
end
puts commas(123)        # "123"
puts commas(1234)       # "1,234"
puts commas(12345)      # "12,435"
puts commas(123456)     # "123,456"
puts commas(1234567)    # "1,234,567"



Create an exponent

puts  31415.0e-4



Create binary numbers by prefacing them with 0b

puts 0b1111



Create floating-point numbers simply by using a decimal point

# you need at least one digit in front of the decimal point
puts  3.1415



create hexadecimal numbers - base 16 - by prefacing them with 0x

puts 0xddff



Create octal number - base eight - numbers by prefacing them with a 0

puts 0355



Make number easier to read by using underscores every three digits from the right

puts 12_345_678_987_654_321