将数字更改为货币和人性化格式

时间:2014-08-12 12:37:50

标签: ruby-on-rails

我正在尝试将某些数字转换为ruby on rails中的货币和人性化格式。

我知道它可以像number_to_currency(number_to_human(4000000))一样完成,但由于某种原因,我试图这样做。

例如。 something like this 4000000.to_s(:human).to_s(:currency) => "$4 Million"

这可能以某种方式?

2 个答案:

答案 0 :(得分:1)

您正在寻找number_to_human

number_to_currency(number_to_human(4000000))

答案 1 :(得分:0)

尝试在en.yml中编写此代码,如下所示:

number:
 currency:
  format:
    delimiter: ! ','
    format: ! '%u%n'
    precision: 2
    separator: .
    significant: false
    strip_insignificant_zeros: false
    unit: $
 format:
  delimiter: ! ','
  precision: 3
  separator: .
  significant: false
  strip_insignificant_zeros: false
 human:
  decimal_units:
    format: ! '%n %u'
    units:
      billion: Billion
      million: Million
      quadrillion: Quadrillion
      thousand: Thousand
      trillion: Trillion
      unit: ''
  format:
    delimiter: ''
    precision: 3
    significant: true
    strip_insignificant_zeros: true

并编写以下代码:

number_to_currency(number_to_human(4000000))

从以下内容中了解number_to_human:link_1link_2