Convert string to decimal number in ruby

后端 未结 4 1015
一生所求
一生所求 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:59

    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

提交回复
热议问题