I have an app in Ruby/Sinatra that uses an API to return a string that I need to format as currency but don't see any simple way to do this.
Specifically, I'd like a string 665778 to print out as $665,778
I tried implementing Sinatra::Numeric::Helpers but that didn't work and I suspect it's outdated. Please advise. There seems easy to do in Rails but not in Sinatra.
I'd like a string 665778 to print out as $665,778
Borrowing the thousands-grouping code from this answer yields a succinct solution:
def number_to_currency(num)
"$#{num.to_s.gsub(/\d(?=(...)+$)/, '\0,')}"
end