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,
41 max(0, math.floor(value_mag / 3) - 1))
43 value = v / pow(10, (suffix_index + 1) * 3)
44 suffix = SI_suffix[suffix_index]
48 return sign + (
"{:." + str(dec) +
"f}").format(value) + suffix
52 Formats number as an SI-prefixed decimal, using binary unit prefixes (ki, Mi, etc.)
54 @param v Number to be formatted
55 @param dec Number of decimal places
57 @returns formatted string
64 SI_suffix =
"kMGTPEZY"
67 value_mag = math.log(v, 2)
69 suffix_index = min(len(SI_suffix) - 1,
70 max(0, math.floor(value_mag / 10) - 1))
72 value = v / pow(2, (suffix_index + 1) * 10)
73 suffix = SI_suffix[suffix_index] +
"i"
77 return sign + (
"{:." + str(dec) +
"f}").format(value) + suffix
82 Formats less-than-one numbers to an SI-prefixed decimal (using prefixes like m, μ)
84 @param v Number to be formatted
85 @param dec Number of decimal places
87 @returns formatted string
93 SI_suffix =
"mμnpfazy"
94 max_log = (len(SI_suffix)) * 3
96 if v >= 1 / pow(10, max_log):
97 value_mag = -1 * math.log10(v)
98 suffix_index = math.ceil(value_mag / 3) - 1
99 value = v * pow(10, (suffix_index + 1) * 3)
100 suffix = SI_suffix[suffix_index]
108 return sign + (
"{:." + str(dec) +
"f}").format(value) + suffix
113 Formats fractional number to SI-prefixed decimal using binary unit prefixes
115 @warning This is not supported
117 return "BinSubValueError"
122 Formats number using full range (will format numbers below 1 with subvalue variant) using SI decimal unit prefixes
124 @param v Number to be formatted
125 @param dec Number of decimal places
127 @returns formatted string
130 return NumberFormatting.formatSISubValue(v, dec)
132 return NumberFormatting.formatSI(v, dec)
136 Generates an expression like "1.0kg/year"
138 @param units amount of units that have happened over measurement interval
139 @param seconds measurement interval, expressed in seconds
140 @param unit name of unit (kg in example above)
142 @returns formatted string, expression like "1.0kg/year"
145 return "-" + NumberFormatting.formatUnitsPerIntervalDynamic(abs(units), seconds, unit)
149 str(
"{:s} is not a valid, positive duration").format(str(seconds)))
151 units_per_second = units / seconds
152 units_per_millisecond = units_per_second / 1000.0
153 units_per_microsecond = units_per_second / 1000000.0
154 units_per_millennium = units_per_second * 1000.0 * 365.0 * 86400.0
155 units_per_century = units_per_second * 100.0 * 365.0 * 86400.0
156 units_per_year = units_per_second * 365.0 * 86400.0
157 units_per_week = units_per_second * 7.0 * 86400.0
158 units_per_day = units_per_second * 86400.0
159 units_per_hour = units_per_second * 3600.0
160 units_per_minute = units_per_second * 60.0
162 if units_per_century < 1.0:
163 return str(
"{:.2f}{:s}/{:s}").format(units_per_millennium, unit,
"millennium")
164 elif units_per_year < 1.0:
165 return str(
"{:.2f}{:s}/{:s}").format(units_per_century, unit,
"century")
166 elif units_per_week < 1.0:
167 return str(
"{:.2f}{:s}/{:s}").format(units_per_year, unit,
"year")
168 elif units_per_day < 1.0:
169 return str(
"{:.2f}{:s}/{:s}").format(units_per_week, unit,
"week")
170 elif units_per_hour < 1.0:
171 return str(
"{:.2f}{:s}/{:s}").format(units_per_day, unit,
"day")
172 elif units_per_minute < 1.0:
173 return str(
"{:.2f}{:s}/{:s}").format(units_per_hour, unit,
"hour")
174 elif units_per_second < 1.0:
175 return str(
"{:.2f}{:s}/{:s}").format(units_per_minute, unit,
"minute")
176 elif units_per_millisecond < 1.0:
177 return str(
"{:.2f}{:s}/{:s}").format(units_per_second, unit,
"second")
178 elif units_per_microsecond < 1.0:
179 return str(
"{:.2f}{:s}/{:s}").format(units_per_millisecond, unit,
"millisecond")
181 return str(
"{:.2f}{:s}/{:s}").format(units_per_microsecond, unit,
"microsecond")