2

I am using Stripe and getting currency that is in pennies. So 12.90 looks like 1290. I want to convert this to U.S. Currency but if I try and use the number_to_currency method it ends up looking like 1,290.00. Is there a method to convert the full string to currency so it comes out correctly at 12.90?

My current code is:

<h4>Total Payment Due: <%= number_to_currency(@user.total_cost)%>
dimakura
  • 7,575
  • 17
  • 36
SupremeA
  • 1,519
  • 3
  • 26
  • 43

2 Answers2

2

You should be able to just divide by 100, but make sure it's a float, ie. use 100.0 to prevent rounding to 12.00.

<h4>Total Payment Due: <%= number_to_currency(@user.total_cost/100.0)%>

That shows $12.90.

mahemoff
  • 44,526
  • 36
  • 160
  • 222
2

You could also use the money gem: https://github.com/RubyMoney/money

Money.new(100, "USD").format #=> "$1.00"

Might be overkill, but will do what you want.

dimakura
  • 7,575
  • 17
  • 36
Tom Fast
  • 1,138
  • 9
  • 15