Last active
March 16, 2019 01:26
-
-
Save agusmakmun/acff983991669bcca74835de591c23c2 to your computer and use it in GitHub Desktop.
Format angka menjadi uang rupiah
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def as_rupiah(uang): | |
""" | |
return a number as rupiah. | |
eg: 2000000 => 2.000.000 | |
""" | |
if uang is None: | |
return 'Rp. 0' | |
return 'Rp. {0:,}'.format(uang).replace(',', '.') | |
""" | |
>>> as_rupiah(2050000) | |
'Rp. 2.050.000' | |
>>> as_rupiah(1) | |
'Rp. 1' | |
>>> as_rupiah(1500) | |
'Rp. 1.500' | |
""" | |
def numberize(number): | |
scales = { | |
1000: 'k', | |
1000000: 'm', | |
1000000000: 'b', | |
1000000000000: 't', | |
1000000000000000: 'quad', | |
1000000000000000000: 'quin', | |
1000000000000000000000: 'sexti', | |
1000000000000000000000000: 'septi', | |
1000000000000000000000000000: 'octi', | |
1000000000000000000000000000000: 'noni', | |
1000000000000000000000000000000000: 'deci', | |
1000000000000000000000000000000000000: 'undeci', | |
1000000000000000000000000000000000000000: 'duodeci', | |
1000000000000000000000000000000000000000000: 'trede', | |
1000000000000000000000000000000000000000000000: 'quattu', | |
1000000000000000000000000000000000000000000000000: 'quindeci', | |
1000000000000000000000000000000000000000000000000000: 'sexdeci', | |
1000000000000000000000000000000000000000000000000000000: 'septend', | |
1000000000000000000000000000000000000000000000000000000000: 'octodeci', | |
1000000000000000000000000000000000000000000000000000000000000: 'novemdeci', | |
1000000000000000000000000000000000000000000000000000000000000000: 'vigintillion', | |
1000000000000000000000000000000000000000000000000000000000000000000: 'infinity' | |
} | |
number = int(number) | |
for digit, name in scales.items(): | |
minimum = '9' * len(str(digit)[1:]) | |
maximum = minimum + '999' | |
if number > max(scales): | |
return "{0:.1f} ".format(number / max(scales)) + name | |
elif number > int(minimum) and number <= int(maximum): | |
return "{0:.1f} ".format(number / digit) + name | |
return number | |
""" | |
>>> numberize(1000) | |
1.0 k | |
>>> numberize(1000000) | |
1.0 m | |
>>> numberize(100120009319931) | |
100.1 t | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment