Rails: Is there a rails trick to adding commas to large numbers?

前端 未结 14 1991
小鲜肉
小鲜肉 2020-12-07 08:19

Is there a way to have rails print out a number with commas in it?

For example, if I have a number 54000000.34, I can run <%= number.function %>, which would prin

14条回答
  •  执笔经年
    2020-12-07 09:02

    The direct way to do this, with or without Rails, is:

    require 'active_support/core_ext/numeric/conversions'
    
    12345.to_s(:delimited)      # => "12,345"
    12345.6789.to_s(:delimited) # => "12,345.6789"
    

    For more options, see Active Support Core Extensions - Numeric - Formatting.

提交回复
热议问题