Skip to content

Instantly share code, notes, and snippets.

@Pinacolada64
Created September 17, 2021 06:20
Show Gist options
  • Save Pinacolada64/6323cda7a96444fda794f56e5d529749 to your computer and use it in GitHub Desktop.
Save Pinacolada64/6323cda7a96444fda794f56e5d529749 to your computer and use it in GitHub Desktop.
Testing comma-delimited numbers and percentages
import time
import logging
# https://docs.python.org/3/library/decimal.html?#recipes
# this does not work for me
def comma_delimited_value(value=0, right_justify=False):
"""
:param value: the decimal value to print (0 - 999,999,999)
:param right_justify: True: print leading spaces to right-justify string (ex., " , ,199"
:return: None
print a comma-delimited decimal value, up to 999,999,999
"""
if 0 > num > 999999999:
return ValueError
width = len(str(num)) # should return how many digits in 'num'
print(f"num = {num}")
print(f"right_justify = {right_justify}")
if right_justify:
if num > 999:
width += 1 # account for thousands separator
if num > 999999:
width += 1 # account for millions separator
print(f"width = {width}")
return " " + str(num)
return str(num)
def comma_value(value, right_justify):
# an expert showed me this:
# >>> print("{:,}".format(3024))
# 3,024
# right-justify numbers:
# >>> print("{:>11}".format("{:,}".format(987654321)))
# 987,654,321
# >>> print("{:>11}".format("{:,}".format(100)))
# 100
"""
Convert int to a comma-delimited string with "," as a grouping separator
Optionally right-justify the output
"""
digits = []
for d in str(value):
digits.append(d)
# reverse list so we can count from tens place, working to the left
digits.reverse()
logging.info(digits)
output = []
count = 0
for d in digits:
# this is really weird, but these next 3 lines need to be in this order to work
count += 1
if count == 4 or count == 7: # and not 3 or 6...
output.append(",")
output.append(d)
# logging.info(f"intermediate: {output}")
# swap the reversed number around again to make it readable:
output.reverse()
# convert list to string:
string = ''.join(output)
logging.info(f"string: {string}")
if right_justify:
return " "[:11 - len(string)] + string
return string
def progress():
maximum = 10000
width = len(str(maximum))
for i in range(maximum+1): # maximum+1 is a hack to get the progress to 100% instead of 90%
if i % 100 == 0:
print(f"\rCompleted {i} of {maximum:{width}} ({i / maximum * 100:3f}%)", flush=True, end='')
time.sleep(0.25)
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG, format='[%(levelname)s] | %(asctime)s | %(message)s')
logging.debug('This is a log message.')
# my old code:
# print(f"Gold: {comma_delimited_value(value=1000, right_justify=True)}")
print(f"Gold: {comma_value(value=10000, right_justify=True)}")
print(f"Large Value: {comma_value(value=987654321, right_justify=False)}")
for thing in ['1', '10', '100', '1000', '10000', '100000', '1000000', '10000000', '100000000']:
print(f"Large Value: {comma_value(value=thing, right_justify=True)}")
for thing in ['1', '10', '100', '1000', '10000', '100000', '1000000', '10000000', '100000000']:
print(f"Large Value: {comma_value(value=thing, right_justify=False)}")
# progress()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment