Skip to content

Instantly share code, notes, and snippets.

@NimaBavari
Last active July 19, 2019 21:38
Show Gist options
  • Save NimaBavari/5598dc1c2cd24cbabf42c48bd7dee470 to your computer and use it in GitHub Desktop.
Save NimaBavari/5598dc1c2cd24cbabf42c48bd7dee470 to your computer and use it in GitHub Desktop.
A Python app that converts amount on bank cheque to text format
"""Reads the amount on a cheque as a string."""
num_sing = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ',
'eight ', 'nine ', 'ten ', 'eleven ', 'twelve ', 'thirteen ',
'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eighteen ',
'nineteen ']
num_tens = ['', 'twenty ', 'thirty ', 'forty ', 'fifty ', 'sixty ', 'seventy ',
'eighty ', 'ninety ']
def get_prefix(amount):
prefix = None
appr_amnt = int(amount)
if amount < 20:
prefix = num_sing[appr_amnt]
elif amount < 100:
sing_conv = int((appr_amnt - appr_amnt % 10) / 10)
prefix = num_tens[sing_conv - 1] + num_sing[appr_amnt % 10]
else:
sing_conv = int((appr_amnt - appr_amnt % 100) / 100)
sing_conv_alt = int((appr_amnt % 100 - appr_amnt % 10) / 10)
prefix = num_sing[sing_conv] + 'hundred ' + \
num_tens[sing_conv_alt - 1] + num_sing[appr_amnt % 10]
return prefix
def get_suffix(amount):
suffix = None
if amount < 1000:
suffix = ''
elif amount < 1000000:
suffix = 'thousand '
elif amount < 1000000000:
suffix = 'million '
elif amount < 1000000000000:
suffix = 'billion '
elif amount < 1000000000000000:
suffix = 'trillion '
elif amount < 1000000000000000000:
suffix = 'quadrillion '
return suffix
def to_text(amnt_on_check):
small_curr = None
major_part = None
max_power_num = 1
small_plch = None
big_curr = amnt_on_check[-3:]
amnt_money = float(amnt_on_check[:-4])
if amnt_money < 1:
major_part = ''
big_plch = ''
else:
sub_amnt = amnt_money
big_plch = big_curr
while sub_amnt > 1:
amnt_replacer = sub_amnt
while sub_amnt / max_power_num >= 1000:
max_power_num *= 1000
amnt_replacer /= 1000
major_part += get_prefix(amnt_replacer) + get_suffix(max_power_num)
sub_amnt -= int(amnt_replacer) * max_power_num
max_power_num = 1
minor_int = int((amnt_money % 1) * 100 + 0.5)
if minor_int == 0:
minor_part = ''
elif minor_int < 20:
minor_part = num_sing[int(minor_int)]
small_plch = small_curr
else:
minor_part = num_tens[int((minor_int - minor_int %
10) / 10 - 1)] + num_sing[int(minor_int % 10)]
small_plch = small_curr
return major_part + big_plch + ' ' + minor_part + small_plch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment