Skip to content

Treat integer-valued floats as their int in get_count - #258

Open
eeshsaxena wants to merge 2 commits into
jaraco:mainfrom
eeshsaxena:fix-get-count-float
Open

Treat integer-valued floats as their int in get_count#258
eeshsaxena wants to merge 2 commits into
jaraco:mainfrom
eeshsaxena:fix-get-count-float

Conversation

@eeshsaxena

Copy link
Copy Markdown

Summary

get_count(1.0) returns 2 instead of 1, so plural("cat", 1.0) incorrectly produces "cats".

The root cause is that get_count compares str(count) against pl_count_one. For a float like 1.0, str(1.0) is "1.0", which is not in pl_count_one, so it falls through to the plural count of 2.

This normalizes integer-valued floats (e.g. 1.0 -> 1) to their int form before the string comparison, so they behave like their int counterpart. Non-integer floats such as 1.5 still count as 2, matching existing behavior.

Before

>>> p.get_count(1.0)
2
>>> p.plural("cat", 1.0)
'cats'

After

>>> p.get_count(1.0)
1
>>> p.plural("cat", 1.0)
'cat'
>>> p.get_count(1.5)   # non-integer float unchanged
2

Tests

Added (1.0, 1), (2.0, 2), (1.5, 2) cases to test_count in tests/test_pwd.py. Full tests/test_pwd.py suite passes (172 passed, 16 xfailed).

Fixes #232

tenfn() accessed mill[mindex] directly in the tens == 1 branch, bypassing
the bounds check in millfn(). number_to_words() on numbers whose leading
group is a teen (e.g. 10**40) therefore leaked an IndexError instead of
the documented NumOutOfRangeError. Route the lookup through millfn().

Fixes jaraco#242
get_count(1.0) returned 2 instead of 1 because str(1.0) is "1.0",
which is not in pl_count_one. As a result plural("cat", 1.0) wrongly
produced "cats". Normalize integer-valued floats (e.g. 1.0 -> 1)
before the string comparison so they behave like their int
counterpart, while non-integer floats such as 1.5 still count as 2.

Fixes jaraco#232
@eeshsaxena eeshsaxena closed this Jul 16, 2026
@eeshsaxena eeshsaxena reopened this Jul 16, 2026
@eeshsaxena

Copy link
Copy Markdown
Author

Hi! Gentle ping on this one; it's been open a couple of weeks. Happy to rebase or make any changes if anything's needed; whenever you get a chance to take a look, I'd really appreciate it. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

get_count returns 2 when the float 1.0 is passed in, this leads to bad pluralizations if you have a float input

1 participant