Note

3桁区切のカンマ(備忘録)

Rubyで3桁区切の日本円カンマをメモ。

class Integer def jpy_comma self.to_s.gsub(/(d)(?=(d{3})+(?!d))/, '1,') end end

numeric = 123456 p numeric.class # 念のため確認 => Fixnum p numeric.methods # 念のため確認 中略 => :jpy_comma

# 実際 p numeric.jpy_comma => "123,456"

# もっと大きい数字 numeric = 12485456309235 p numeric.jpy_ccomma => "12,485,456,309,235"

# 少数点のある数字 (エラーが出てほしい) b = 13754.4566 => 13754.4566 b.jpy_comma NoMethodError: undefined method `jpy_comma' for 13754.4566:Float

よいよい。