how to display price with symbol using money-rails

2019-09-02 13:13发布

Given this simple Money object query

Money.new(1000, "USD").to_s
=> "10.00" 

How can I display the value with its symbol? I'm aware I can call money_object.symbol but some currencies place the symbol before and other after the value. Im pretty sure there should be some easy method already for this? Haven't find it by reading into the documentation.

2条回答
何必那么认真
2楼-- · 2019-09-02 14:13

ActionView::Helpers::NumberHelper has number_to_currency which should do the trick.

查看更多
手持菜刀,她持情操
3楼-- · 2019-09-02 14:19

if you are using money-rails, you have a lots of helpers:

  • the currency_symbol helper method

    <%= currency_symbol %>
    

This will render a span dom element with the default currency symbol.

  • the humanized_money helper method

    <%= humanized_money @money_object %>
    

This will render a formatted money value without the currency symbol and without the cents part if it contains only zeros (uses :no_cents_fi_whole flag).

  • humanize with symbol helper

    <%= humanized_money_with_symbol @money_object %>
    

This will render a formatted money value including the currency symbol and without the cents part if it contains only zeros.

  • get the money value without the cents part

    <%= money_without_cents @money_object %>
    

This will render a formatted money value without the currency symbol and without the cents part.

  • get the money value without the cents part and including the currency symbol

    <%= money_without_cents_and_with_symbol @money_object %>
    

This will render a formatted money value including the currency symbol and without the cents part.

查看更多
登录 后发表回答