3
2

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.

漢数字とアラビア数字の相互変換

Last updated at Posted at 2017-07-07

先日、「ローマ数字とアラビア数字の互換」について投稿しました。
http://qiita.com/xuyun19840618/items/512600a37db522589cee

本日、漢数字とアラビアの互換について、自己流を紹介します。

単位

漢数字の単位をメイン単位とサブ単位に分けます。
###メイン単位
10の4乗で一区切りとして、メイン単位は下記になります。

漢数字 arabic
10の0乗
10の4乗
10の8乗
10の12乗
10の16乗
10の20乗
10の24乗
10の28乗
10の32乗
10の36乗
10の40乗
10の44乗
10の48乗
恒河沙 10の52乗
阿僧祇 10の56乗
那由他 10の60乗
不可思議 10の64乗
無量大数 10の68乗

###サブ単位

漢数字 arabic
1
10
100
1000

##説明
###アラビア数字から漢数字へ

例:12345678

12345678 = 1234 * 10^4 + 5678 * 10^0 = 1234 万 + 5678

1234 = 1 * 1000 + 2 * 100 + 3 * 10 + 4 = 千二百三十四

同じく
5678 = 五千六百七十八

なので、
12345678 = 千二百三十四万五千六百七十八

###漢数字からアラビア数字へ
例:千二百三十四万五千六百七十八

千二百三十四万五千六百七十八 = 千二百三十四 万 五千六百七十八

そのうち、
千二百三十四 = 1000 + 2 * 100 + 3 * 10 + 4 = 1234
五千六百七十八 = 5678

なので、
千二百三十四万五千六百七十八 = 1234 * 10^4 + 5678 = 12345678

演習

>>> ArabicToJapanese(324562123124688965059)
>>> 三垓二千四百五十六京二千百二十三兆千二百四十六億八千八百九十六万五千五十九

>>> ArabicToJapanese('IV')
>>> 'error'

>>> JapaneseToArabic('一京')
>>> 10000000000000000

コード

Jap_Arab.py
japanese_1 = '','','','','','','','','','','','','','','','','',''
# 無は無量大数 不は不可思議 那は那由他 阿は阿僧祇 恒は恒河沙
japanese_2 = '','','',''
japanese_3 = '','','','','','','','','',''
japanese_4 = '無量大数','不可思議','那由他','阿僧祇','恒河沙'

lenth = len(japanese_1), len(japanese_2), len(japanese_3)

def ArabicToJapanese(arabic):
    
    if not isinstance(arabic, int) or arabic <= 0:
        return 'error'
    japanese = ''
    for j_1 in japanese_1:
        index_1 = japanese_1.index(j_1)
        a_1 = 10 ** ( (lenth[0] - 1 - index_1) * 4 )
        n = arabic // a_1
        if n > 0:
            arabic %= a_1
            for j_2 in japanese_2:
                index_2 = japanese_2.index(j_2)
                a_2 = 10 ** (lenth[1] - 1 - index_2)
                k = n // a_2
                if k > 0:
                    if k == 1 and a_2 != 1:
                        japanese += j_2
                    else:
                        index_3 = lenth[2] -1 - k
                        japanese += japanese_3[index_3] + j_2
                    n %= a_2
            japanese += j_1
    for a,b in zip(japanese_4,japanese_1[0:5]):
        japanese = japanese.replace(b,a)
        
    return japanese


def JapaneseToArabic(japanese):
    
    if not isinstance(japanese, str):
        return 'error'
    for a,b in zip(japanese_4,japanese_1[0:5]):
        japanese = japanese.replace(a,b)
    for j in japanese:
        if j not in japanese_1 and j not in japanese_2 and j not in japanese_3:
            return 'error'
        
    value = 0
    for j_1 in japanese_1:
        if j_1 in japanese:
            index = japanese.index(j_1)
            if index != 0:
                ja = japanese[0 : index]
                japanese = japanese[(index+1) : ]
            else:
                ja = japanese[0 : ]              
            value_1 = 0
            for j_2 in japanese_2:
                if j_2 in ja:
                    ix = ja.index(j_2)
                    if ix != 0:
                        value_2 = lenth[2] -1 - japanese_3.index(ja[0 : ix])
                        ja = ja[(ix+1) : ]
                    else:
                        if j_2 != '':
                            value_2 = 1
                            ja = ja[(ix+1) : ]
                        if j_2 == '':
                            value_2 = lenth[2] -1 - japanese_3.index(ja)
                    value_1 += value_2 * 10 ** (lenth[1] - 1 - japanese_2.index(j_2))
            value += value_1 * 10 ** ( (lenth[0] - 1 - japanese_1.index(j_1)) * 4 )
            
    return value
3
2
4

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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?