Skip to main content

例題.数字の3桁区切り(toCurrency)

Python

py
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:]

Bash

sh
printf "%'d\n" 12345678

Ruby

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

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

rb
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 の場合

rb
number_to_currency(12345678)