Jump to content

Talk:Z13588

From Wikifunctions
Latest comment: 10 months ago by 99of9 in topic Removing unnecessary "and"

Removing unnecessary "and"

Sorry, I don't have permission to edit the function itself, but it was adding "and" at the start of larger numbers. Specifically, "and" is now added after "hundred" only when there's a remainder (e.g., 901 becomes "nine hundred and one"). If you change the second half of it to:


```

def Z13587(Z13587K1):

    # Define the units and tens place

"""

Keep this part of the code as is...

"""

# Function to convert numbers less than 1000 to words

    def words(Z13587K1):

        if Z13587K1 == 0:

            return ""

        elif Z13587K1 <= 19:

            return ones[Z13587K1]

        elif Z13587K1 <= 99:

            tens_unit = tens[Z13587K1 // 10]

            ones_unit = ones[Z13587K1 % 10]

            return tens_unit + "-" + ones_unit if ones_unit else tens_unit

        else:

            remainder = Z13587K1 % 100

            remainder_text = " and " + words(remainder) if remainder else ""

            return ones[Z13587K1 // 100] + "hundred" + remainder_text

    # Convert numbers to words

    if Z13587K1 == 0:

        return "zero"

    elif Z13587K1 < 0:

        return "negative " + Z13587(-Z13587K1)

    else:

        result = ""

        for unit_name, factor in units:

            count, Z13587K1 = divmod(Z13587K1, factor)

            if count > 999:

                raise ValueError("Number is too large for this algorithm")

            if count > 0:

                if factor == 1:

                    result = result + " and " + words(count) + unit_name

                else:

                    result = result + " " + words(count) + unit_name

        result = result.strip()

        if result.startswith("and "):

            result = result[4:]  # Remove leading "and"

        return result.strip()

``` Jbd0912 (talk) 06:34, 21 January 2025 (UTC)Reply

Good pickup, thanks. I added a test which the previous code failed, and copied in your code, with one tiny modification to make sure there was a space in the right place. --99of9 (talk) 11:44, 21 January 2025 (UTC)Reply