Rails 怎么改设置让 number_to_currency 怎么显示成人民币或者其他货币?

jiwoorico · 2013年10月30日 · 最后由 wwwicbd 回复于 2017年11月23日 · 4972 次阅读

最近正在学习 Agile Web Development with Rails,看到一行自动把数字转化成货币的代码,<%= number_to_currency(product.price) %>,现在默认输出是美元,请问哪里改设置才能显示成人民币或者其他货币?

谢谢

config.i18n.default_locale = 'zh-CN'

#1 楼 @kingwkb 这句话加在哪里?

config/application.rb

#3 楼 @kingwkb 我加了没用啊。

The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.

# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] # config.i18n.default_locale = :de config.i18n.default_locale = 'zh-CN'

number_to_currency(1234567890.506, precision: 3) # => $1,234,567,890.506
number_to_currency(1234567890.506, locale: :fr)  # => 1 234 567 890,51 €

感觉你根本懒得看文档

:locale - Sets the locale to be used for formatting (defaults to current locale).

#5 楼 @Victor 那么对于人民币locale: 后面应该填什么?我找不到关于 locale 文档呢?

http://guides.rubyonrails.org/i18n.html https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale

1 楼说的 zh-CN 是没错。但是你的问题是没有建立 zh-CN 的翻译文件。或者你不指定 zh-CN 也可以。按照下面网址的回复,把 en 文件改改也行。

http://stackoverflow.com/questions/1400268/drying-views-in-rails-number-to-currency

victor 回复

数字习惯每四位分隔一次,如何用 number_to_currency 实现这个功能呢?

需要 登录 后方可回复, 如果你还没有账号请 注册新账号