def number_format(num, places=0): """Format a number with grouped thousands and given decimal places""" places = max(0,places) tmp = "%.*f" % (places, num) point = tmp.find(".") integer = (point == -1) and tmp or tmp[:point] decimal = (point != -1) and tmp[point:] or "" count = 0 formatted = [] for i in range(len(integer), 0, -1): count += 1 formatted.append(integer[i - 1]) if count % 3 == 0 and i - 1: formatted.append(",") integer = "".join(formatted[::-1]) return integer+decimal