def round(value, decimal=0, digits=None): """ ROUND TO GIVEN NUMBER OF DIGITS, OR GIVEN NUMBER OF DECIMAL PLACES decimal - NUMBER OF DIGITS AFTER DECIMAL POINT (NEGATIVE IS VALID) digits - NUMBER OF SIGNIFICANT DIGITS (LESS THAN 1 IS INVALID) """ if value == None: return None elif value == 0: return 0 else: value = float(value) if digits != None: try: if digits <= 0: return sign(value) * pow(10, round(math_log10(abs(value)), 0)) m = pow(10, math_ceil(math_log10(abs(value)))) return _round(value / m, 0) * m except Exception as e: from mo_logs import Log Log.error("not expected", e) elif decimal <= 0: return int(_round(value, decimal)) else: return _round(value, decimal)
def percent(value, decimal=None, digits=None, places=None): """ display value as a percent (1 = 100%) :param value: :param decimal: :param digits: :param places: :return: """ if value == None: return "" value = float(value) if value == 0.0: return "0%" digits = coalesce(digits, places) if digits != None: left_of_decimal = int(math.ceil(math.log10(abs(value)))) + 2 decimal = digits - left_of_decimal decimal = coalesce(decimal, 0) right_of_decimal = max(decimal, 0) format = "{:." + text(right_of_decimal) + "%}" return format.format(_round(value, decimal + 2))
def round(value, decimal=7, digits=None): """ ROUND TO GIVEN NUMBER OF DIGITS, OR GIVEN NUMBER OF DECIMAL PLACES decimal - NUMBER OF DIGITS AFTER DECIMAL POINT (NEGATIVE IS VALID) digits - NUMBER OF SIGNIFICANT DIGITS (LESS THAN 1 IS INVALID) """ if value == None: return None else: value = float(value) if digits != None: if digits <= 0: if value == 0: return int(_round(value, digits)) try: m = pow(10, math.ceil(math.log10(abs(value)))) return int(_round(value / m, digits) * m) except Exception as e: from mo_logs import Log Log.error("not expected", e) else: if value == 0: return _round(value, digits) try: m = pow(10, math.ceil(math.log10(abs(value)))) return _round(value / m, digits) * m except Exception as e: from mo_logs import Log Log.error("not expected", e) elif decimal <= 0: return int(_round(value, decimal)) else: return _round(value, decimal)
def comma(value): """ FORMAT WITH THOUSANDS COMMA (,) SEPARATOR """ try: if float(value) == _round(float(value), 0): output = "{:,}".format(int(value)) else: output = "{:,}".format(float(value)) except Exception: output = text_type(value) return output
def comma(value): """ FORMAT WITH THOUSANDS COMMA (,) SEPARATOR """ try: if float(value) == _round(float(value), 0): output = "{:,}".format(int(value)) else: output = "{:,}".format(float(value)) except Exception: output = text_type(value) return output
def percent(value, decimal=None, digits=None, places=None): value = float(value) if value == 0.0: return "0%" digits = coalesce(digits, places) if digits != None: left_of_decimal = int(math.ceil(math.log10(abs(value)))) + 2 decimal = digits - left_of_decimal decimal = coalesce(decimal, 0) right_of_decimal = max(decimal, 0) format = "{:." + text_type(right_of_decimal) + "%}" return format.format(_round(value, decimal + 2))
def round(value, decimal=None, digits=None, places=None): """ :param value: THE VALUE TO ROUND :param decimal: NUMBER OF DECIMAL PLACES TO ROUND (NEGATIVE IS LEFT-OF-DECIMAL) :param digits: ROUND TO SIGNIFICANT NUMBER OF digits :param places: SAME AS digits :return: """ value = float(value) if value == 0.0: return "0" digits = coalesce(digits, places) if digits != None: left_of_decimal = int(math.ceil(math.log10(abs(value)))) decimal = digits - left_of_decimal right_of_decimal = max(decimal, 0) format = "{:." + text_type(right_of_decimal) + "f}" return format.format(_round(value, decimal))
def round(value, decimal=None, digits=None, places=None): """ :param value: THE VALUE TO ROUND :param decimal: NUMBER OF DECIMAL PLACES TO ROUND (NEGATIVE IS LEFT-OF-DECIMAL) :param digits: ROUND TO SIGNIFICANT NUMBER OF digits :param places: SAME AS digits :return: """ value = float(value) if value == 0.0: return "0" digits = coalesce(digits, places) if digits != None: left_of_decimal = int(math.ceil(math.log10(abs(value)))) decimal = digits - left_of_decimal right_of_decimal = max(decimal, 0) format = "{:." + text_type(right_of_decimal) + "f}" return format.format(_round(value, decimal))
def replacer(found): ops = found.group(1).split("|") path = ops[0] var = path.lstrip(".") depth = min(len(seq), max(1, len(path) - len(var))) try: val = seq[-depth] if var: if is_sequence(val) and float(var) == _round(float(var), 0): val = val[int(var)] else: val = val[var] for func_name in ops[1:]: parts = func_name.split("(") if len(parts) > 1: val = eval(parts[0] + "(val, " + "(".join(parts[1::])) else: val = FORMATTERS[func_name](val) val = toString(val) return val except Exception as e: from mo_logs import Except e = Except.wrap(e) try: if e.message.find("is not JSON serializable"): # WORK HARDER val = toString(val) return val except Exception as f: if not _Log: _late_import() _Log.warning( "Can not expand " + "|".join(ops) + " in template: {{template_|json}}", template_=template, cause=e, ) return "[template expansion error: (" + str(e.message) + ")]"
def replacer(found): ops = found.group(1).split("|") path = ops[0] var = path.lstrip(".") depth = min(len(seq), max(1, len(path) - len(var))) try: val = seq[-depth] if var: if is_sequence(val) and float(var) == _round(float(var), 0): val = val[int(var)] else: val = val[var] for func_name in ops[1:]: parts = func_name.split('(') if len(parts) > 1: val = eval(parts[0] + "(val, " + ("(".join(parts[1::]))) else: val = FORMATTERS[func_name](val) val = toString(val) return val except Exception as e: from mo_logs import Except e = Except.wrap(e) try: if e.message.find("is not JSON serializable"): # WORK HARDER val = toString(val) return val except Exception as f: if not _Log: _late_import() _Log.warning( "Can not expand " + "|".join(ops) + " in template: {{template_|json}}", template_=template, cause=e ) return "[template expansion error: (" + str(e.message) + ")]"
def percent(value, decimal=None, digits=None, places=None): """ display value as a percent (1 = 100%) :param value: :param decimal: :param digits: :param places: :return: """ value = float(value) if value == 0.0: return "0%" digits = coalesce(digits, places) if digits != None: left_of_decimal = int(math.ceil(math.log10(abs(value)))) + 2 decimal = digits - left_of_decimal decimal = coalesce(decimal, 0) right_of_decimal = max(decimal, 0) format = "{:." + text_type(right_of_decimal) + "%}" return format.format(_round(value, decimal + 2))