|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | | -import math |
6 | | - |
7 | 5 | from .i18n import _gettext as _ |
8 | 6 | from .i18n import _ngettext, decimal_separator, thousands_separator |
9 | 7 | from .i18n import _ngettext_noop as NS_ |
|
25 | 23 |
|
26 | 24 | def _format_not_finite(value: float) -> str: |
27 | 25 | """Utility function to handle infinite and nan cases.""" |
| 26 | + import math |
| 27 | + |
28 | 28 | if math.isnan(value): |
29 | 29 | return "NaN" |
30 | 30 | if math.isinf(value) and value < 0: |
@@ -71,6 +71,8 @@ def ordinal(value: NumberOrString, gender: str = "male") -> str: |
71 | 71 | Returns: |
72 | 72 | str: Ordinal string. |
73 | 73 | """ |
| 74 | + import math |
| 75 | + |
74 | 76 | try: |
75 | 77 | if not math.isfinite(float(value)): |
76 | 78 | return _format_not_finite(float(value)) |
@@ -142,6 +144,8 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str: |
142 | 144 | Returns: |
143 | 145 | str: String containing commas every three digits. |
144 | 146 | """ |
| 147 | + import math |
| 148 | + |
145 | 149 | thousands_sep = thousands_separator() |
146 | 150 | decimal_sep = decimal_separator() |
147 | 151 | try: |
@@ -226,6 +230,8 @@ def intword(value: NumberOrString, format: str = "%.1f") -> str: |
226 | 230 | str: Friendly text representation as a string, unless the value passed could not |
227 | 231 | be coaxed into an `int`. |
228 | 232 | """ |
| 233 | + import math |
| 234 | + |
229 | 235 | try: |
230 | 236 | if not math.isfinite(float(value)): |
231 | 237 | return _format_not_finite(float(value)) |
@@ -293,6 +299,8 @@ def apnumber(value: NumberOrString) -> str: |
293 | 299 | returns a string unless the value was not `int`-able, then `str(value)` |
294 | 300 | is returned. |
295 | 301 | """ |
| 302 | + import math |
| 303 | + |
296 | 304 | try: |
297 | 305 | if not math.isfinite(float(value)): |
298 | 306 | return _format_not_finite(float(value)) |
@@ -354,6 +362,8 @@ def fractional(value: NumberOrString) -> str: |
354 | 362 | Returns: |
355 | 363 | str: Fractional number as a string. |
356 | 364 | """ |
| 365 | + import math |
| 366 | + |
357 | 367 | try: |
358 | 368 | number = float(value) |
359 | 369 | if not math.isfinite(number): |
@@ -408,6 +418,8 @@ def scientific(value: NumberOrString, precision: int = 2) -> str: |
408 | 418 | Returns: |
409 | 419 | str: Number in scientific notation z.wq x 10ⁿ. |
410 | 420 | """ |
| 421 | + import math |
| 422 | + |
411 | 423 | exponents = { |
412 | 424 | "0": "⁰", |
413 | 425 | "1": "¹", |
@@ -491,6 +503,8 @@ def clamp( |
491 | 503 | will be prepended with a token indicating as such. |
492 | 504 |
|
493 | 505 | """ |
| 506 | + import math |
| 507 | + |
494 | 508 | if value is None: |
495 | 509 | return None |
496 | 510 |
|
@@ -554,6 +568,8 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str: |
554 | 568 | Returns: |
555 | 569 | str: |
556 | 570 | """ |
| 571 | + import math |
| 572 | + |
557 | 573 | if not math.isfinite(value): |
558 | 574 | return _format_not_finite(value) |
559 | 575 | exponent = int(math.floor(math.log10(abs(value)))) if value != 0 else 0 |
|
0 commit comments