Posted at

Ruby snippet of Corporate Number conversion

More than 1 year has passed since last update.

This article is a translation from .


This is a ruby snippet which converts an old-style 12-digit enterprise identification number in Japan to a new-style 13-digit “Corporate Number” adding a check digit as a leftmost digit.

# Old-style 12-digit number, defining as a string because it can possibly starts with 0

n12 = "000012050002"

# New-style 13-digit number
n13 = (9 - n12.split("").map(&:to_i).map.with_index {|d, i|
i.odd?? d : d * 2 }.inject(:+) % 9).to_s + n12
# => "7000012050002"