LoginSignup
3
1

More than 3 years have passed since last update.

railsで数値を3桁で区切る。「delimited」メソッドで解決

Posted at

数字を3桁で区切る方法を解決しましたので備忘録として。
例:1000 → 1,000

仕様

Rails 5.1.7
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]

やりたいこと

productsテーブルのpriceカラムを表示させたいです。
↓productsテーブル
EX-IT.png

「delimited」メソッドで解決

sample.html
<%= product.price.to_s(:delimited)%>
#例 => 12,345,678

「delimiter」オプションで任意の文字で桁を区切れます。

sample.html
<%= product.price.to_s(:delimited, delimiter: "あ")%>
#例 => 12あ345あ678
3
1
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
3
1