Created
July 24, 2017 09:17
-
-
Save dev-drprasad/55c9408dc5a2376507e6e51c33f9888d to your computer and use it in GitHub Desktop.
python code to convert float values to indian money format
This file contains 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
import locale | |
from math import log10 | |
def sexify(value): | |
""" | |
formats the float value to indian money format | |
:param value: | |
:return: | |
""" | |
MAP = { | |
0: '', | |
1: '', | |
2: '', | |
3: 'Thousand', | |
4: 'Thousand', | |
5: 'Lakh', | |
6: 'Lakh', | |
7: 'Crore', | |
8: 'Crore', | |
} | |
power = int(log10(abs(value))) | |
locale.setlocale(locale.LC_ALL, 'en_IN') | |
if power <= 8: | |
humanized = locale.format('%d', value, grouping=True) | |
rounded = humanized[:4].replace(',', '.') | |
s = rounded + ' ' + MAP[power] | |
if (rounded != '1.00'): s = s + 's' | |
else: | |
value = value / 10000000 | |
humanized = locale.format('%d', value, grouping=True) | |
s = humanized + ' ' + 'Crores' | |
return s | |
if __name__ == '__main__': | |
sexified = sexify(10970300.00) | |
print sexified |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment