Convert string to decimal number in ruby Convert string to decimal number in ruby ruby ruby

Convert string to decimal number in ruby


You call to_i, you get integer.

Try calling to_f, you should get a float.

For more string conversion methods, look here.


If you want more accurate answer with the calculation to avoid bug like this https://www.codecademy.com/en/forum_questions/50fe886f68fc44056f00626c you can use conversion to decimalexample :

require 'bigdecimal'require 'bigdecimal/util'size = ARGV[0]size = size.to_d

this should make the printed number become decimal but if you want to make it as float again just put this to_f again

size=size.to_fputs size


You also can use the decimal class for it

a = '2.45'Decimal(a) # => 2.45

UPDATE:

Use bigdecimal as @bigtex777 mentioned:

source: http://ruby-doc.org/stdlib-2.2.2/libdoc/bigdecimal/rdoc/BigDecimal.html