Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: remove decimal from number_to_currency

I have a float price:

number_to_currency(m.price, :locale => 'en_us')

I get: $39.00

How can I remove .00, I want get:

$39
like image 366
hyperrjas Avatar asked Dec 12 '12 11:12

hyperrjas


2 Answers

You can set the precision to 0 as documented here in the Rails API Docs.

number_to_currency(m.price, locale: :en, precision: 0)

Be aware that your prices will be rounded, anything from $38.50 to $39.49 will be displayed as $39.

Edit: Exchanged locale :en_us for :en, which might be enabled in more apps.

like image 187
Thomas Klemm Avatar answered Oct 06 '22 10:10

Thomas Klemm


Assuming that you want to skip decimals only in case of 0 cents, how about

number_to_currency(m.price, :locale => 'en_us').gsub(/\.00$/, "")
like image 35
claasz Avatar answered Oct 06 '22 10:10

claasz