18 from decimal
import Decimal, ROUND_HALF_UP
21 def mathematicalRound(value: float, decimalPlaces: int) -> float:
23 Performes mathematical (ROUND_HALF_UP) rounding
24 Ex. >= 1.5 will be rounded to 2, < 1.5 will be rounded to 1
31 amount of decimal places to which the value will be rounded
35 float -> the rounded value
38 decimal = Decimal(str(value))
39 places = Decimal(10) ** -decimalPlaces
41 return float(decimal.quantize(places, rounding = ROUND_HALF_UP))
44 def formatBytes(value: int, precision: int = 2) -> str:
46 Formats a size in bytes into a human-readable format
47 Ex. value 1444 will be formatted as 1.44 KB
54 number of decimal places (default is 2)
58 str -> formatted string with size and appropriate unit
61 suffixes = [
"B",
"KB",
"MB",
"GB",
"TB",
"PB",
"EB",
"ZB",
"YB"]
64 adjustedValue = float(value)
66 while adjustedValue >= unitBase
and index < len(suffixes) - 1:
68 adjustedValue /= unitBase
70 return f
"{adjustedValue:.{precision}f} {suffixes[index]}"