0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby snippet of Corporate Number conversion

Posted at

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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?