how to display price with symbol using money-rails

♀尐吖头ヾ 提交于 2019-12-12 03:48:49

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/13634084/how-to-display-price-with-symbol-using-money-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!