Skip to content

Instantly share code, notes, and snippets.

@danielSanchezQ
Created December 21, 2017 09:25
Show Gist options
  • Save danielSanchezQ/7c5b4730d06c86b80f11dc0d0e7b66c4 to your computer and use it in GitHub Desktop.
Save danielSanchezQ/7c5b4730d06c86b80f11dc0d0e7b66c4 to your computer and use it in GitHub Desktop.
Number to vietnamese representation
from itertools import *
vietNums = {
0 : u"",
1 : u"một",
2 : u"hai",
3 : u"ba",
4 : u"bốn",
5 : u"năm",
6 : u"sáu",
7 : u"bảy",
8 : u"tám",
9 : u"chín",
10 : u"mười",
100 : u"trăm",
1000: u"nghìn"
}
levels = {
0 : u"",
1000 : u"nghìn",
1000000 : u"triệu",
1000000000 : u"tỉ"
}
def numToVietPrime(n):
level = 0
ret = []
while n > 0:
n, rem = divmod(n, 10)
ret.append("{} {}".format(vietNums[rem], vietNums[level]))
level = level * 10 if level else 10
return " ".join(reversed(ret))
def numToViet(n):
nums = map(lambda x: numToVietPrime(int(str(n//x)[-3:])) if x > 0 else numToVietPrime(n%1000),
reversed(sorted(levels.keys())))
res = map(lambda x, y: "{} {}".format(x, y[1]) if x else "", nums, reversed(sorted(levels.items())))
return " ".join(res).replace(" ", " ").lstrip()
print(numToViet(2345677))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment