14

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

Hommer Smith
  • 26,772
  • 56
  • 167
  • 296

2 Answers2

36
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
deefour
  • 34,974
  • 7
  • 97
  • 90
  • 1
    For anyone looking at this answer in 2022, there is now a specific option to keep or strip trailing zeros called `strip_insignificant_zeros`. So you can do something like `number_to_currency(amount, precision: 2, strip_insignificant_zeros: false)` – DemitryT Mar 07 '22 at 17:20
  • @DemitryT as far as I can tell, that still doesn't solve the use-case listed above: `ActiveSupport::NumberHelper.number_to_currency(101.00, precision: 2, strip_insignificant_zeros: false)` gives me `"$101.00"` as opposed to `"$101"` – Daniel Dao Apr 04 '23 at 22:37
  • @DanielDao Sorry I flipped the boolean, just change it to `strip_insignificant_zeros: true` to get the expected outcome. `ActiveSupport::NumberHelper.number_to_currency(101.00, precision: 2, strip_insignificant_zeros: true)` => "$101" – DemitryT Apr 05 '23 at 19:25
  • @DemitryT I think that works for 101.00, but for the use-case in the original question: `30.50`, it'll strip the `0` so it becomes `30.5`. Example: `ActiveSupport::NumberHelper.number_to_currency(30.50, precision: 2, strip_insignificant_zeros: true)` yields `"$30.5"` as opposed to `$30.50`. – Daniel Dao Apr 05 '23 at 20:12
  • Yes, that's expected behavior. The original use case was "but if I have 30.00, I want to delete those zeros", which this works for. 30.00 => 30, 30.50 => 30.5, 30.05 => 30.05 Highly recommend you read the docs: https://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_to_currency If you want $30.50 returned, that's different behavior. – DemitryT Apr 05 '23 at 21:57
  • Hey @DemitryT , thanks for directing me to the docs. Maybe I am wrong and I did read the docs, but I think this is in the OP's (original stackoverflow question): "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." Based on what you're saying "If you want $30.50 returned, that's different behavior." this doesn't solve that, right? – Daniel Dao Apr 05 '23 at 23:48
  • Yes, but as you can see in the accepted answer, the two values for the fix are $30.00 and $30.05, which `strip_insignificant_zeros: true` would work for. If you also want $30.50 to be handled, then you can use the accepted answer. In reality, most users won't care about seeing $30.5, it's still the right value and displays fine. While $30.00 does not display as nicely, hence this fix is fine. All in all, this is just a convenient way to not hack anything together while getting 95% there. – DemitryT Apr 06 '23 at 15:53
0

I use money strings , so I do it a different way :

def string_to_cents str
    new_str = number_to_currency(str,  :format => "%n")
    if new_str && new_str[-3..-1] == ".00"
        new_str[-3..-1] = ""
    end
    new_str
end
Jon
  • 555
  • 5
  • 5