Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 680
Jess Avatar asked Oct 08 '22 04:10

Jess


1 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 => ',') %>
like image 140
John Topley Avatar answered Oct 12 '22 12:10

John Topley