Search code examples
ruby-on-railsnumber-to-currency

Convert pennies to currency with rails


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)%>

Solution

  • 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.