Skip to content

Commit

Permalink
Merge branch 'python-humanize:main' into precisedelta-rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
nuztalgia authored Jul 11, 2022
2 parents 181031e + e705e43 commit e2f2dad
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
File renamed without changes.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v2.32.0
rev: v2.34.0
hooks:
- id: pyupgrade
args: [--py37-plus]

- repo: https://github.com/psf/black
rev: 22.3.0
rev: 22.6.0
hooks:
- id: black
args: [--target-version=py37]
Expand Down Expand Up @@ -42,7 +42,7 @@ repos:
- id: python-check-blanket-noqa

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
rev: v4.3.0
hooks:
- id: check-json
- id: check-merge-conflict
Expand All @@ -58,7 +58,7 @@ repos:
files: "src/"

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.942
rev: v0.961
hooks:
- id: mypy
additional_dependencies: [pytest, types-freezegun, types-setuptools]
Expand All @@ -71,7 +71,7 @@ repos:
args: [--max-py-version=3.11]

- repo: https://github.com/tox-dev/pyproject-fmt
rev: 0.3.3
rev: 0.3.4
hooks:
- id: pyproject-fmt

Expand Down
6 changes: 3 additions & 3 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mkdocs>=1.1
mkdocs==1.3.0
mkdocs-material
mkdocstrings[python]>=0.18
mkdocstrings[python]==0.19.0
mkdocs-include-markdown-plugin
pygments
pymdown-extensions>=9.2
pymdown-extensions==9.5
21 changes: 17 additions & 4 deletions src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,23 +205,36 @@ def intword(value: NumberOrString, format: str = "%.1f") -> str:
except (TypeError, ValueError):
return str(value)

if value < 0:
value *= -1
negative_prefix = "-"
else:
negative_prefix = ""

if value < powers[0]:
return str(value)
return negative_prefix + str(value)
for ordinal, power in enumerate(powers[1:], 1):
if value < power:
chopped = value / float(powers[ordinal - 1])
if float(format % chopped) == float(10**3):
chopped = value / float(powers[ordinal])
singular, plural = human_powers[ordinal]
return (
" ".join([format, _ngettext(singular, plural, math.ceil(chopped))])
negative_prefix
+ " ".join(
[format, _ngettext(singular, plural, math.ceil(chopped))]
)
) % chopped
else:
singular, plural = human_powers[ordinal - 1]
return (
" ".join([format, _ngettext(singular, plural, math.ceil(chopped))])
negative_prefix
+ " ".join(
[format, _ngettext(singular, plural, math.ceil(chopped))]
)
) % chopped
return str(value)

return negative_prefix + str(value)


def apnumber(value: NumberOrString) -> str:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,31 @@ def test_intword_powers() -> None:
@pytest.mark.parametrize(
"test_args, expected",
[
(["0"], "0"),
(["100"], "100"),
(["-100"], "-100"),
(["1000"], "1.0 thousand"),
(["12400"], "12.4 thousand"),
(["12490"], "12.5 thousand"),
(["1000000"], "1.0 million"),
(["-1000000"], "-1.0 million"),
(["1200000"], "1.2 million"),
(["1290000"], "1.3 million"),
(["999999999"], "1.0 billion"),
(["1000000000"], "1.0 billion"),
(["-1000000000"], "-1.0 billion"),
(["2000000000"], "2.0 billion"),
(["999999999999"], "1.0 trillion"),
(["1000000000000"], "1.0 trillion"),
(["6000000000000"], "6.0 trillion"),
(["-6000000000000"], "-6.0 trillion"),
(["999999999999999"], "1.0 quadrillion"),
(["1000000000000000"], "1.0 quadrillion"),
(["1300000000000000"], "1.3 quadrillion"),
(["-1300000000000000"], "-1.3 quadrillion"),
(["3500000000000000000000"], "3.5 sextillion"),
(["8100000000000000000000000000000000"], "8.1 decillion"),
(["-8100000000000000000000000000000000"], "-8.1 decillion"),
([None], "None"),
(["1230000", "%0.2f"], "1.23 million"),
([10**101], "1" + "0" * 101),
Expand Down

0 comments on commit e2f2dad

Please sign in to comment.