# 例題. 数字の3桁区切り(to Currency)

# Bash

printf "%'d\n" 12345678

# Python

print re.sub(r'(\d{3})(?=\d)', r'\1,', str(12345678)[::-1])[::-1]

```py

locale.setlocale(locale.LC_ALL, '')
print locale.currency(12345678, grouping=True)[2:]

# Ruby

12345678.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\1,').reverse

Integer の拡張にしてしまう場合

class Integer
    def to_currency()
        self.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\1,').reverse
    end
end

puts 12345678.to_currency  # => 12,345,678

Rails の場合

number_to_currency(12345678)

[ 📩 ご意見 ]