Sunday, September 15, 2024
STEM

Numbers to spoken words

How do you say this number: 8129311136456.25? Not easy even in your native language, and far less so in a foreign one. But don’t worry, in this blog I share the solution. The number, no matter how gnarly, you enter will be converted to a spoken word…even in foreign languages.

Okay, before I reveal the solution, the answer to the above number is: eight trillion, one hundred and twenty-nine billion, three hundred and eleven million, one hundred and thirty-six thousand, four hundred and fifty-six point two five.

My solution was to use a proven, tested library that’s in the Python community. I initially wrote my version but it was becoming a chore beyond 4 digits. So, using num2words library seemed like a wise choice…not only did it save me time, it also is well-tested to be robust and supports many languages. Best of all, it works even for decimal inputs. For full documentation, check out its page.

The Setup:

In order to run the code, you’ll need to install ‘num2words’ library in your Python environment: pip install num2words

The code:

The API is really straight-forward. The language options and other parameters are listed in the documentation page.

Examples:

Here are some outputs from the code above. Unfortunately, the code cannot be hosted here using my usual tkinter widgets as they don’t allow custom library uploads and they don’t inherently support this library in their Cloud environment, so you’ll have to run it locally or configure it elsewhere yourself to run it remotely.

The different inputs in the shell are shown in blue texts above. The outputs by the code follow immediately after. You can see the expressions were translated into Spanish and French as well. Also, you can see that it handles decimal inputs just fine.

To get solutions in the opposite direction, that is, to convert spoken/written expression of a number to numeric digits only see my older blog under Related section below.

Hope you found it helpful.


Related:

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top
+