Reputation: 7391
I'm looking for function to echo price like this:
17.0 #=> 17
17.5 #=> 17.50
number_to_currency
works in very familiar fashion, but still echos $17.00
if there are no cents :(
Upvotes: 2
Views: 240
Reputation: 76784
You'll be looking for strip_insignificant_zeros
, like so:
number_to_currency("17.00", strip_insignificant_zeros: false) #-> $17.00
number_to_currency("17.00", strip_insignificant_zeros: true) #-> $17
Upvotes: 1
Reputation: 1904
If you still want to use the number_to_currency
then one way you can have what you want work would be:
number_to_currency(17.0).chomp(".00") => "$17"
number_to_currency(17.5).chomp(".00") => "$17.50"
That way it strips the ending ".00" if present
chomp(*args)
: "Returns a new String with the given record separator removed from the end of str (if present)." http://apidock.com/ruby/String/chomp
Upvotes: 2