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

intword with negative numbers #12

Closed
maxrioux100 opened this issue May 2, 2022 · 5 comments · Fixed by #41
Closed

intword with negative numbers #12

maxrioux100 opened this issue May 2, 2022 · 5 comments · Fixed by #41
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@maxrioux100
Copy link

maxrioux100 commented May 2, 2022

What did you do?

import humanize
print(humanize.intword(-1000000000))

What did you expect to happen?

-1.0 billion

What actually happened?

-1000000000

What versions are you using?

  • OS: Windows 10
  • Python: 3.10.4
  • Humanize: 3.14
@hugovk
Copy link
Member

hugovk commented May 19, 2022

Thanks, I'd be open to a PR.

@hugovk hugovk added good first issue Good for newcomers help wanted Extra attention is needed labels May 19, 2022
@maxrioux100
Copy link
Author

maxrioux100 commented May 19, 2022

import humanize
def with_negative(number):
    negative = number < 0
    if negative : number *= -1
    word = humanize.intword(number)
    if negative : word = f'-{word}'
    return word
print(with_negative(-1000000000)) 

Something like that (I wrote it in the browser without testing so it may have some typos)

@carterbox
Copy link
Contributor

By opening a PR, hugovk means open a pull-request with some changes to the source code of humanize, not writing a new function that wraps around the existing humanize API.

@maxrioux100
Copy link
Author

def intword(value, format="%.1f"):
    """Converts a large integer to a friendly text representation.
    Works best for numbers over 1 million. For example, 1_000_000 becomes "1.0 million",
    1200000 becomes "1.2 million" and "1_200_000_000" becomes "1.2 billion". Supports up
    to decillion (33 digits) and googol (100 digits).
    Examples:
        ```pycon
        >>> intword("100")
        '100'
        >>> intword("12400")
        '12.4 thousand'
        >>> intword("1000000")
        '1.0 million'
        >>> intword(1_200_000_000)
        '1.2 billion'
        >>> intword(8100000000000000000000000000000000)
        '8.1 decillion'
        >>> intword(None) is None
        True
        >>> intword("1234000", "%0.3f")
        '1.234 million'
        ```
    Args:
        value (int, float, str): Integer to convert.
        format (str): To change the number of decimal or general format of the number
            portion.
    Returns:
        str: Friendly text representation as a string, unless the value passed could not
        be coaxed into an `int`.
    """
    try:
        value = int(value)
    except (TypeError, ValueError):
        return value

    negative = value < 0
    if negative : value *= -1
    
    if value < powers[0]:
        return 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))])
                ) % chopped
            else:
                singular, plural = human_powers[ordinal - 1]
                return (
                    " ".join([format, _ngettext(singular, plural, math.ceil(chopped))])
                ) % chopped

     if negative : value = f'-{value}'

    return str(value)

It was just to help you. Here is a better version. Not on my computer so I don't have access to git

@maxrioux100
Copy link
Author

@pytest.mark.parametrize(
    "test_args, expected",
    [
        (["100"], "100"),
        (["1000"], "1.0 thousand"),
        (["12400"], "12.4 thousand"),
        (["12490"], "12.5 thousand"),
        (["1000000"], "1.0 million"),
        (["1200000"], "1.2 million"),
        (["1290000"], "1.3 million"),
        (["999999999"], "1.0 billion"),
        (["1000000000"], "1.0 billion"),
        (["2000000000"], "2.0 billion"),
        (["999999999999"], "1.0 trillion"),
        (["1000000000000"], "1.0 trillion"),
        (["6000000000000"], "6.0 trillion"),
        (["999999999999999"], "1.0 quadrillion"),
        (["1000000000000000"], "1.0 quadrillion"),
        (["1300000000000000"], "1.3 quadrillion"),
        (["3500000000000000000000"], "3.5 sextillion"),
        (["8100000000000000000000000000000000"], "8.1 decillion"),
        (["-100"], "-100"),
        (["-1000"], "-1.0 thousand"),
        (["-12400"], "-12.4 thousand"),
        (["-12490"], "-12.5 thousand"),
        (["-1000000"], "-1.0 million"),
        (["-1200000"], "-1.2 million"),
        (["-1290000"], "-1.3 million"),
        (["-999999999"], "-1.0 billion"),
        (["-1000000000"], "-1.0 billion"),
        (["-2000000000"], "-2.0 billion"),
        (["-999999999999"], "-1.0 trillion"),
        (["-1000000000000"], "-1.0 trillion"),
        (["-6000000000000"], "-6.0 trillion"),
        (["-999999999999999"], "-1.0 quadrillion"),
        (["-1000000000000000"], "-1.0 quadrillion"),
        (["-1300000000000000"], "-1.3 quadrillion"),
        (["-3500000000000000000000"], "-3.5 sextillion"),
        (["-8100000000000000000000000000000000"], "-8.1 decillion"),
        ([None], None),
        (["1230000", "%0.2f"], "1.23 million"),
        ([10**101], "1" + "0" * 101),
    ],
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants