Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

from __future__ import annotations

__lazy_modules__ = {"bisect"}
__lazy_modules__ = {"bisect", "math"}

import bisect
import math

from .i18n import _gettext as _
from .i18n import _ngettext, decimal_separator, thousands_separator
Expand Down Expand Up @@ -52,8 +53,6 @@

def _format_not_finite(value: float) -> str:
"""Utility function to handle infinite and nan cases."""
import math

if math.isnan(value):
return "NaN"
if math.isinf(value) and value < 0:
Expand Down Expand Up @@ -100,8 +99,6 @@ def ordinal(value: NumberOrString, gender: str = "male") -> str:
Returns:
str: Ordinal string.
"""
import math

try:
if not math.isfinite(float(value)):
return _format_not_finite(float(value))
Expand Down Expand Up @@ -147,8 +144,6 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
Returns:
str: String containing commas every three digits.
"""
import math

thousands_sep = thousands_separator()
decimal_sep = decimal_separator()
try:
Expand Down Expand Up @@ -228,8 +223,6 @@ def intword(value: NumberOrString, format: str = "%.1f") -> str:
str: Friendly text representation as a string, unless the value passed could not
be coaxed into an `int`.
"""
import math

try:
if not math.isfinite(float(value)):
return _format_not_finite(float(value))
Expand Down Expand Up @@ -295,8 +288,6 @@ def apnumber(value: NumberOrString) -> str:
returns a string unless the value was not `int`-able, then `str(value)`
is returned.
"""
import math

try:
if not math.isfinite(float(value)):
return _format_not_finite(float(value))
Expand Down Expand Up @@ -347,8 +338,6 @@ def fractional(value: NumberOrString) -> str:
Returns:
str: Fractional number as a string.
"""
import math

try:
number = float(value)
if not math.isfinite(number):
Expand Down Expand Up @@ -410,8 +399,6 @@ def scientific(value: NumberOrString, precision: int = 2) -> str:
Returns:
str: Number in scientific notation z.wq x 10ⁿ.
"""
import math

try:
value = float(value)
if not math.isfinite(value):
Expand Down Expand Up @@ -473,8 +460,6 @@ def clamp(
will be prepended with a token indicating as such.

"""
import math

if value is None:
return None

Expand Down Expand Up @@ -538,8 +523,6 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str:
Returns:
str:
"""
import math

if not math.isfinite(value):
return _format_not_finite(value)
exponent = math.floor(math.log10(abs(value))) if value != 0 else 0
Expand Down
Loading