41
36

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails - 通貨フォーマットの扱い

Last updated at Posted at 2015-07-05

ドルの通貨フォーマットに変換

<%= number_to_currency(123456789) %>

→ $123,456,789.00

小数点5つまで表示

<%= number_to_currency(123456789, :precision => 5) %>

→ $123,456,789.00000

小数点をスペースで区切る

<%= number_to_currency(123456789, :separator => " ") %>

→ $123,456,789 00

桁区切りをスペース

<%= number_to_currency(123456789, :delimiter => " ") %>

→ $123 456 789.00

通過単位を「¥」

<%= number_to_currency(123456789, :unit => "¥") %>

→ ¥123,456,789.00

通過単位を「JPY」

<%= number_to_currency(123456789, :unit => "JPY") %>

→ 123,456,789.00JPY
※ この場合は、後ろにつくので注意

通貨単位を後ろ

<%= number_to_currency(123456789, :format => "%u%n", :unit => "¥") %>

→ 123,456,789.00円
ここでの「%u」はunitの略、また「%n」はnumberの略
入れ替えれば、順番も逆になる

マイナスのフォーマット

<%= number_to_currency(-123456789, :negative_format => "%u-%n") %>

→ $-123,456,789.00

円のロケールを設定

# config/locales/jp/yml
jp:
  number:
    currency:
      format:
        unit: "円"
        format: "%n%u"
        negative_format: "-%n%u"
        precision: 0
<%= number_to_currency(123456789, :locale => 'jp') %>

→ 123,456,789円


参考URL
http://railsdoc.com/references/number_to_currency

41
36
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
41
36

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?