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

Viewed 90724

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 print out "54,000,000.34"

thanks!

16 Answers

You want the number_with_delimiter method. For example:

<%= number_with_delimiter(@number, :delimiter => ',') %>

Alternatively, you can use the number_with_precision method to ensure that the number is always displayed with two decimal places of precision:

<%= number_with_precision(@number, :precision => 2, :delimiter => ',') %>

Yes, use the NumberHelper. The method you are looking for is number_with_delimiter.

 number_with_delimiter(98765432.98, :delimiter => ",", :separator => ".")
 # => 98,765,432.98

new syntax

number_with_delimiter(@number, delimiter: ",")

If you you want to user delimeter for money then you can do

number_to_currency(@number)

this will add $ too. If you are using money gem then you can do

Money.new(@number,"USD").format

This will also put $.

number_with_delimiter

ruby money

number_to_currency

For Ruby guys: Formatting numbers (integers only) with a comma separator between every group of thousands.

number = 12345678
numStr1 = number.to_s.reverse.scan(/.{1,3}/).join(',').reverse
puts numStr1             # => 12,345,678

numStr2 = number.to_s.gsub(/\B(?=(...)*\b)/, ',')
puts numStr2             # => 12,345,678
  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

The following do the trick for both delimiter and precision (API ref).

ActiveSupport::NumberHelper.number_to_rounded(1234.532, delimiter: ',', precision: 1) 
     

(or from views just number_to_rounded, no need for the prefix)

HTH

I had this challenge when working on a Rails 6 application.

If the number is for the price of an item or has to do with currency, then you can use number_to_currency ActionView Helper

Here's how to do it:

number_to_currency("123456789")                      # => $123456789
number_to_currency(1234567890.50)                    # => $1,234,567,890.50
number_to_currency(1234567890.506)                   # => $1,234,567,890.51
number_to_currency(1234567890.506, precision: 3)     # => $1,234,567,890.506
number_to_currency(1234567890.506, locale: :fr)      # => 1 234 567 890,51 €
number_to_currency(1234567890.50, unit: '₦', delimiter: ',', precision: 0)    # => ₦1,234,567,890
number_to_currency(1234567890.50, unit: "R$", separator: ",", delimiter: "")  # => R$1234567890,50

You can read up more about it here in the Rails documentation: number_to_currency

That's all.

I hope this helps

For Ruby Folks: functions can be created to set comma to large number integer.

def number_with_comma(numStr)
   return numStr.to_s.gsub(/\B(?=(...)*\b)/, ',')
end
a = number_with_comma 1234567
puts a   => 1,234,567

x = 9876543
y = number_with_comma x
puts y   => 9,876,543
Related