Skip to content

Instantly share code, notes, and snippets.

@ikegami-yukino
Created September 3, 2015 02:55
Show Gist options
  • Save ikegami-yukino/edd3573a9f25d4d64442 to your computer and use it in GitHub Desktop.
Save ikegami-yukino/edd3573a9f25d4d64442 to your computer and use it in GitHub Desktop.
Convert Arabic numerals to Chinese numerals
CHINESE_MAP = {'1': '一', '2': '二', '3': '三', '4': '四', '5': '五', '6': '六', '7': '七', '8': '八', '9': '九'}
CHINESE_DIGITS = ('十', '百', '千', '万', '十万', '百万', '千万', '億', '十億', '百億', '千億', '兆', '十兆', '百兆', '千兆')
def arabic2chinese(arabic):
chinese = []
if len(arabic) == '0':
return '〇'
arabic = arabic.replace(',', '')
for (i, num) in enumerate(arabic[::-1]):
if num == '0':
continue
elif i > 0:
chinese.append(CHINESE_DIGITS[i-1])
chinese.append(CHINESE_MAP[num])
return ''.join(chinese[::-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment