我正在使用 Stripe 并获得以便士为单位的货币。所以 12.90 看起来像 1290。我想将其转换为美国货币,但如果我尝试使用number_to_currency方法,它最终看起来像 1,290.00。有没有一种方法可以将完整的字符串转换为货币,使其在 12.90 时正确显示?
我目前的代码是:
<h4>Total Payment Due: <%= number_to_currency(@user.total_cost)%>
我正在使用 Stripe 并获得以便士为单位的货币。所以 12.90 看起来像 1290。我想将其转换为美国货币,但如果我尝试使用number_to_currency方法,它最终看起来像 1,290.00。有没有一种方法可以将完整的字符串转换为货币,使其在 12.90 时正确显示?
我目前的代码是:
<h4>Total Payment Due: <%= number_to_currency(@user.total_cost)%>
你应该能够除以 100,但要确保它是一个浮点数,即。使用 100.0 来防止舍入到 12.00。
<h4>Total Payment Due: <%= number_to_currency(@user.total_cost/100.0)%>
这表明$12.90
。
您也可以使用金钱宝石:https ://github.com/RubyMoney/money
Money.new(100, "USD").format #=> "$1.00"
可能有点矫枉过正,但会做你想做的事。