LoginSignup
8
5

More than 5 years have passed since last update.

数字を通貨表示にする

Posted at

数字を通貨表示(コンマをつける)とかってどうするんやろう?と思い
調べてみたら簡単だったので覚書です。

課題:skull:

下記の記載だとコンマが入らず数字が大きくなった時に読みづらい

.individual__items__item__middle__price
  ¥
  =@item.price
%span.individual__items__item__middle__price__tax
 (税込)
%span.individual__items__item__middle__price__text
  =@item.fee_side

スクリーンショット 2019-04-13 18.26.41.png

解決:angel:

rubyのヘルパーメソッド、number_to_currencyを使用して解決!

.individual__items__item__middle__price
  = number_to_currency(@item.price, unit: "¥", strip_insignificant_zeros: true)
%span.individual__items__item__middle__price__tax
  (税込)
%span.individual__items__item__middle__price__text
  =@item.fee_side

スクリーンショット 2019-04-13 18.27.19.png

①ヘルパーメソッドの記述
=@item.price

= number_to_currency(@item.price)
コンマがつくが、小数点まで表示される(9,768.00となる)

②小数点以下を消す
= number_to_currency(@item.price, strip_insignificant_zeros: true)

③通貨マークを表示させる
= number_to_currency(@item.price, unit: "¥", strip_insignificant_zeros: true)

参考記事

http://railsdoc.com/references/number_to_currency
http://railsdoc.com/references/number_to_rounded
(公式)
https://qiita.com/noriaki/items/d6c0503e7d25c7329bf7
(他にもやり方色々あるようです!)

8
5
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
8
5