Gediminas Šukys
Gediminas Šukys

Reputation: 7391

Human readable price in Rails

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

Answers (2)

Richard Peck
Richard Peck

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

rorofromfrance
rorofromfrance

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

Related Questions