Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Number to currency, delete trailing zeros

How can I use number_to_currency and make it delete the zeros in the decimal part?

So if I have a number 30.50, I want to keep the .50, but if I have 30.00, I want to delete those zeros. I see the precision, but I don't know if I can use it conditionally to just be applied if the trailing decimals are zeros...

Thanks

like image 857
Hommer Smith Avatar asked Jul 26 '12 02:07

Hommer Smith


1 Answers

num = 30.00
number_to_currency(num, :precision => (num.round == num) ? 0 : 2)
  => $30

num = 30.05
number_to_currency(num, :precision => (num.round == num) ? 0 : 2)
  => $30.05
like image 72
deefour Avatar answered Oct 12 '22 22:10

deefour