Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to force stop some groups in the string #198

Open
ayrtondenner opened this issue Jul 27, 2023 · 1 comment
Open

How to force stop some groups in the string #198

ayrtondenner opened this issue Jul 27, 2023 · 1 comment

Comments

@ayrtondenner
Copy link

Let's say that I have the number "41721", and I want my result in groups of two, BUT I want to group the first and last two digits of the number. In that case, my desired result is: "forty-one, seven, twenty-one".

Is there any way to force that? I saw an example in documentation using underscore, but couldn't find more explanation to that. So maybe:

import inflect
p = inflect.engine()
p.number_to_words("41_7_21", group=2)
p.number_to_words("41-7-21", group=2)
p.number_to_words("41 7 21", group=2)
...
@jaraco
Copy link
Owner

jaraco commented Apr 23, 2024

For the record, what that currently outputs is:

>>> p.number_to_words("41_7_21", group=2)
'forty-one, seventy-two, one'
>>> p.number_to_words("41-7-21", group=2)
'forty-one, seventy-two, one'
>>> p.number_to_words("41 7 21", group=2)
'forty-one, seventy-two, one'

I did discover I can get close to what you're seeking using a decimal:

>>> p.number_to_words("41.7.21", group=2)
'forty-one, point, seven, point, twenty-one'

And of course, you could repair the output:

>>> p.number_to_words("41.7.21", group=2).replace('point, ', '')
'forty-one, seven, twenty-one'

To be sure, that feels brittle.

Given the current implementation is already 150 lines, there's not a good abstraction for honoring stop groups.

Since you already have to go to the trouble of splitting them, why not use ', '.join(map(p.number_to_words, (41, 7, 21)))?

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

No branches or pull requests

2 participants