Convert string to decimal number in ruby

ElektroStudios picture ElektroStudios · Mar 31, 2012 · Viewed 78.4k times · Source

I need to work with decimals. In my program, the user need to put a number with decimals to convert that number.

The problem is: If I try to convert the argument into a number I get a integer without decimals.

# ARGV[0] is: 44.33

size = ARGV[0]

puts size.to_i
# size is: 44
# :(

Answer

Sergio Tulentsev picture Sergio Tulentsev · Mar 31, 2012

You call to_i, you get integer.

Try calling to_f, you should get a float.

For more string conversion methods, look here.