alias
String#to_f (Ruby)
alias lang_id
ruby
how to
convert string to float
lang_id
ruby
qid
28004

How to
String#to_f (Ruby)

Redirected to “convert string to float

Use String#to_f method.

s = "3.14159"
x = s.to_f
print x          # 3.14159
print x.class    # Float

s = "abc"
x = s.to_f
print x          # 0.0
print x.class    # Float

Or use Float(arg) function.

s = "-3.14159"
x = Float(s)
print x         # -3.14159

s = "abc"
x = Float(s)  # ArgumentError is raised
Select default language: