2 This module provides various string formatters 10 This exception is raised whenever provided duration is zero or negative. 18 This class provides various number formats 23 Formats number as an SI-prefixed decimal 25 @param v Number to be formatted 26 @param dec Number of decimal places 28 @returns formatted string 35 SI_suffix =
"kMGTPEZY" 38 value_mag = math.log10(v)
40 suffix_index = min(len(SI_suffix) - 1, max(0, math.floor(value_mag / 3) - 1))
42 value = v / pow(10, (suffix_index + 1) * 3)
43 suffix = SI_suffix[suffix_index]
47 return sign + (
"{:." + str(dec) +
"f}").format(value) + suffix
51 Formats number as an SI-prefixed decimal, using binary unit prefixes (ki, Mi, etc.) 53 @param v Number to be formatted 54 @param dec Number of decimal places 56 @returns formatted string 63 SI_suffix =
"kMGTPEZY" 66 value_mag = math.log(v, 2)
68 suffix_index = min(len(SI_suffix) - 1, max(0, math.floor(value_mag / 10) - 1))
70 value = v / pow(2, (suffix_index + 1) * 10)
71 suffix = SI_suffix[suffix_index] +
"i" 75 return sign + (
"{:." + str(dec) +
"f}").format(value) + suffix
80 Formats less-than-one numbers to an SI-prefixed decimal (using prefixes like m, μ) 82 @param v Number to be formatted 83 @param dec Number of decimal places 85 @returns formatted string 91 SI_suffix =
"mμnpfazy" 92 max_log = (len(SI_suffix)) * 3
94 if v >= 1 / pow(10, max_log):
95 value_mag = -1 * math.log10(v)
96 suffix_index = math.ceil(value_mag / 3) - 1
97 value = v * pow(10, (suffix_index + 1) * 3)
98 suffix = SI_suffix[suffix_index]
106 return sign + (
"{:." + str(dec) +
"f}").format(value) + suffix
111 Formats fractional number to SI-prefixed decimal using binary unit prefixes 113 @warning This is not supported 115 return "BinSubValueError" 120 Formats number using full range (will format numbers below 1 with subvalue variant) using SI decimal unit prefixes 122 @param v Number to be formatted 123 @param dec Number of decimal places 125 @returns formatted string 128 return NumberFormatting.formatSISubValue(v, dec)
130 return NumberFormatting.formatSI(v, dec)
134 Generates an expression like "1.0kg/year" 136 @param units amount of units that have happened over measurement interval 137 @param seconds measurement interval, expressed in seconds 138 @param unit name of unit (kg in example above) 140 @returns formatted string, expression like "1.0kg/year" 143 return "-" + NumberFormatting.formatUnitsPerIntervalDynamic(abs(units), seconds, unit)
148 units_per_second = units / seconds
149 units_per_millisecond = units_per_second / 1000.0
150 units_per_microsecond = units_per_second / 1000000.0
151 units_per_millenium = units_per_second * 1000.0 * 365.0 * 86400.0
152 units_per_century = units_per_second * 100.0 * 365.0 * 86400.0
153 units_per_year = units_per_second * 365.0 * 86400.0
154 units_per_week = units_per_second * 7.0 * 86400.0
155 units_per_day = units_per_second * 86400.0
156 units_per_hour = units_per_second * 3600.0
157 units_per_minute = units_per_second * 60.0
159 if units_per_century < 1.0:
160 return str(
"{:.2f}{:s}/{:s}").format(units_per_millenium, unit,
"millenium")
161 elif units_per_year < 1.0:
162 return str(
"{:.2f}{:s}/{:s}").format(units_per_century, unit,
"century")
163 elif units_per_week < 1.0:
164 return str(
"{:.2f}{:s}/{:s}").format(units_per_year, unit,
"year")
165 elif units_per_day < 1.0:
166 return str(
"{:.2f}{:s}/{:s}").format(units_per_week, unit,
"week")
167 elif units_per_hour < 1.0:
168 return str(
"{:.2f}{:s}/{:s}").format(units_per_day, unit,
"day")
169 elif units_per_minute < 1.0:
170 return str(
"{:.2f}{:s}/{:s}").format(units_per_hour, unit,
"hour")
171 elif units_per_second < 1.0:
172 return str(
"{:.2f}{:s}/{:s}").format(units_per_minute, unit,
"minute")
173 elif units_per_millisecond < 1.0:
174 return str(
"{:.2f}{:s}/{:s}").format(units_per_second, unit,
"second")
175 elif units_per_microsecond < 1.0:
176 return str(
"{:.2f}{:s}/{:s}").format(units_per_millisecond, unit,
"millisecond")
178 return str(
"{:.2f}{:s}/{:s}").format(units_per_microsecond, unit,
"microsecond")