Skip to content

Fix stray space before punctuation andword in number_to_words#247

Open
gaoflow wants to merge 1 commit into
jaraco:mainfrom
gaoflow:fix-229-andword-space
Open

Fix stray space before punctuation andword in number_to_words#247
gaoflow wants to merge 1 commit into
jaraco:mainfrom
gaoflow:fix-229-andword-space

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 16, 2026

Copy link
Copy Markdown

Fixes #229.

number_to_words(541140, andword=",") produced five hundred, forty-one thousand, one hundred , forty with a stray space before the final comma. The same happens for any number whose last group has trailing tens or units, for example number_to_words(140, andword=",") gave one hundred , forty.

Cause

In _handle_chunk, the code first runs WHITESPACES_COMMA.sub(",", chunk) to drop any space before a comma. The later COMMA_WORD.sub(f" {andword} \\1", chunk) then re-introduces a leading space through its space-padded template. With a word andword (the default "and") those surrounding spaces are correct, but with a punctuation andword such as "," the leading space is wrong.

Fix

Re-apply the existing WHITESPACES_COMMA cleanup immediately after the COMMA_WORD substitution, scoped to the branch that performs it. The default "and" and other word andword values are unchanged.

Verification

  • python -m pytest tests/test_pwd.py -k "andword or number_to_words or numwords" -q
  • python -m pytest -q (214 passed, 16 xfailed)
  • python -m compileall inflect tests
  • python -m ruff format --check inflect/init.py tests/test_pwd.py
  • git diff --check upstream/main...HEAD

Note: python -m ruff check inflect/__init__.py tests/test_pwd.py currently reports pre-existing UP006/UP007/UP035 modernization diagnostics across inflect/__init__.py unrelated to this change.

Disclosure: I prepared this fix with AI assistance under my direction; I reviewed and verified the change and tests myself.

When andword is punctuation such as ",", the COMMA_WORD substitution
re-introduced a space before it, producing "one hundred , forty" instead
of "one hundred, forty". Re-apply the existing WHITESPACES_COMMA cleanup
after the substitution so the separator attaches to the preceding word.

Fixes jaraco#229.
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.

Why a extra blank appeared in calling number_to_words to convert 541140 to words with andword=','

1 participant