Convert string to decimal number in ruby

后端 未结 4 1056
一生所求
一生所求 2021-02-02 05:28

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

4条回答
  •  [愿得一人]
    2021-02-02 05:44

    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 decimal example :

    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_f
    
    puts size
    

提交回复
热议问题