Rails: Number to currency, delete trailing zeros
Asked Answered
S

2

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

Spheroid answered 26/7, 2012 at 2:46 Comment(0)
B
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
Berners answered 26/7, 2012 at 2:51 Comment(7)
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)Pekan
@Pekan 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"Angarsk
@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"Pekan
@Pekan 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.Angarsk
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: api.rubyonrails.org/classes/ActionView/Helpers/… If you want $30.50 returned, that's different behavior.Pekan
Hey @Pekan , 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?Angarsk
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.Pekan
P
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
Pritchard answered 22/4, 2015 at 23:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.