13
9

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.

【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

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?