Ruby/Number/Convert to Float

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

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

Содержание

Convert string to float

"3.14".to_f                     # => 3.14
"1.602e-19".to_f                # => 1.602e-19



create or convert floating-point numbers with Kernel"s Float method

puts Float(167) # convert an integer
puts Float("77") # convert a string
puts Float(a) # convert a character code



the percentage

((1.to_f / 2.to_f) * 100).to_i



try weird conversions

puts "15".to_f
puts "99.999".to_f
puts "99.999".to_i
puts ""
puts "5 is!".to_i
puts "about 5".to_i
puts "Your".to_f
puts ""
puts "stringy".to_s
puts 3.to_i