Ruby
MyNumber
CorporateNumber

Ruby snippet of Corporate Number conversion

This article is a translation from https://qiita.com/kawasaki/items/fc1abfd62db10fa082ca .


Snippet

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" 

Reference