如果@service.amount
等于 28.95 美元:
<p><%= number_to_currency(@service.amount) %></p>
输出 2895.00 美元,而不是我上面写的。搜索后,我没有找到解决方案。
@service.amount
是一个整数,因为 Stripe 只接受整数。
如果@service.amount
等于 28.95 美元:
<p><%= number_to_currency(@service.amount) %></p>
输出 2895.00 美元,而不是我上面写的。搜索后,我没有找到解决方案。
@service.amount
是一个整数,因为 Stripe 只接受整数。
Stripe 将值存储为美分。从文档:
零十进制货币
所有 API 请求都希望以货币的最小单位提供金额。例如,要收取 10 美元,请提供 1000 的金额值(即 1000 美分)。
我假设 API响应的工作方式相同。
要获得正确的输出,您必须将美元值除以 100,例如:
<%= number_to_currency(@service.amount.fdiv(100)) %>
还有Money gem,它可能是一个更好的选择。它存储值(以美分表示)和货币,并带有格式:
require 'money'
money = Money.new(@service.amount, @service.currency)
#=> #<Money fractional:2895 currency:USD>
money.format
#=> "$28.95"
number_to_currency
不期望以美分获得金额。它认为是美元。您需要做的是将美分金额转换为美元,然后将其发送到number_to_currency
方法。
我不知道对象是什么,@service
但您应该能够创建另一个名为amount_in_dollars
:
def amount_in_dollars
amount / 100.to_d
end
然后在数字货币方法中使用它:
<p><%= number_to_currency(@service.amount_in_dollars) %></p>
或者您可以直接在视图中划分它(但我更喜欢第一个变体)
<p><%= number_to_currency(@service.amount / 100.to_d) %></p>