Search code examples
ruby-on-railsnumber-to-currency

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


Solution

  • 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