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

前端 未结 14 1995
小鲜肉
小鲜肉 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:12

      def add_commas(numstring)
        correct_idxs = (1..100).to_a.select{|n| n % 6 == 0}.map{|n| n - 1}
         numstring.reverse.chars.join(",").chars.select.with_index{|x, i| i.even? || correct_idxs.include?(i)}.join.reverse
      end
    

    This was my way in ruby

    Addition edit: Basically it adds all commas in between the numbers and only selects the ones where the index + 1 % 6

    I figured the commas up to 100 was fine but if you want a super long number just make 100 a higher number

提交回复
热议问题