LoginSignup
12
9

More than 3 years have passed since last update.

【Ruby】7桁の整数で保存した郵便番号にハイフン(-)を入れる方法

Last updated at Posted at 2019-11-12

1234567のように、DBに7桁の整数で郵便番号を保存。
一方、ビューでは123-4567のようにハイフンを入れて表示したい場合。

以下のように記述すればOKです。

1234567.to_s.insert(3, "-")

to_sで整数を文字列に変換。
insert(3, "-")で3文字目の後にハイフンを挿入しています。

参考になった記事
https://qiita.com/_kkoichi/items/f8758a438b2363943922

12
9
2

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
12
9