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

前端 未结 14 1993
小鲜肉
小鲜肉 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 08:54

    If you're doing it a lot but also FYI because it's not implied by the above, Rails has sensible defaults for the number_with_delimiter method.

    #inside controller or view
    number_with_delimiter(2444323.4)
    #=> 2,444,323.30
    
    #inside console
    helper.number_with_delimiter(233423)
    #=> 233,423
    

    No need to supply the delimiter value if you're doing it the most typical way.

提交回复
热议问题