Number To Words

18 minutes
Share the link to this page
Copied
  Completed

Transcript

Hello, and welcome. My name is Pato Majumdar. I have worked on developing telecom billing solutions for more than 20 years. One problem, which requires to be solved in developing any billing solution is to convert numbers to words. Normally, this is seen as a simple problem. However, the problem does not remain so simple, when it has to be done according to different standards, which are followed by different countries.

For example, European countries or American countries read numbers in thousands. However, when it comes to India, we have hundreds then thousand 10,010 lakh 10 lakh crore 10 crore etc. Another dimension to this problem is when numbers have to be written in words in different languages. Now every language has its own words for numbers, and this makes the problem quite complicated. However, if we have the base algorithm for doing this job, we can apply it in almost any language for any convention. In this video, we discuss how to write this program using our language.

Normally, R is not a suitable language for this programming. However, I go on to demonstrate that our language can be used for solving these kind of problems as well. We will discuss the complete code of this problem and see some variations as well. So sit back and relax while we go through this discussion. So let us put in context, what we are going to develop we are going to develop a function Using our language, the name of our function will be numbers to words. The function will take our input as an integer is greater than zero, and will output a string, which will be the words for the integer input.

The example is given here. Let's look at the basic structure of our code. We will write a function numbers to words which will take our integer as a input. There will be a helper function, which will actually do the translation from numbers to words. We'll have a trim function which will do some beautification and make number function which will convert digits two numbers. Then we will disable scientific notation We will call the helper function to do the job given here is the code for the helper function.

So first we convert the number into digits and reverse the order, then we count the number of digits. If the number of digits is one, we display the words equivalent of that and we carry on like this for the remaining of the words. So here you see we are also considering how to display hundreds. And then when we have larger numbers, we make a generic function, which will do the job for us. Take a look at the code. When we run it, it will become more clear.

Now we look at the code for the trim function. The trim function is simple. It uses regular expressions to get rid of white spaces leading or trailing Another unnecessary characters that we do not want appearing as the output. Now that we have defined our helper functions, let's get back to the code for numbers to words. So, we first define some vectors once once will contain all the single digit numbers in words. So we have 01234 up to nine, defined under once, look at the work function called for names.

Names once, zero to nine, means that we are naming the individual positions of the vector once. So once of zero will give zero. Similarly once of two will give the word two and so on. Similarly, we have teens, where teens have one will give 11 look at the suffixes. This particular program has a limitation that it can only express numbers of two trillions later we will see a code which has no such restrictions. Lastly, we round off X to remove any decimal places.

And if the length of x that is if there are more than one numbers provided, then we apply this helper function on each of the numbers. We will see how this works. Now that we have gone through the code, let us see it in action. So here we are in our studio. You can see that we have the function numbers towards we have the helper function. Then we have the trim function like we have discussed.

And we have the make numbers function. We defined the vectors once, which contains all the single digit numbers. we name it using names. So that the positions are named according to the numbers, we do the same for teams stance and recreate a vector of suffixes as well. So here we can go up to trillions. We round off x, remove any decimals, and we call the helper for all the numbers of x if there are more than one provider.

Lastly, we call the helper if there is only one x provided we see the helper function a little bit more. So we split the number into digits and reverse it, because the number of digits if the number of digits is one, then we straightaway take it from the vector and display the number if it is to give you a little bit more work. So the code is provided here where we take the teens and 10 separately, then we have the code for handling the hundreds. And then we have the code for handling all of the numbers. So now that you've seen the code that's compiled it. Now, you see it is available the function is available for use.

Clear the console and start trying with different numbers to see if it works. So you see that it is working for this particular number. We try with different values and see if the function works. So our function seems to be working, although there are flaws in this function, I would like you to find out what his flaws are. For example, try the number 100 and see what you get. Try some really large values So, in general the function works, but like I told you, it has got flaws, I would like you to find out the flaws.

So we saw the working of the numbers towards program for the western system. Now let's see how we can make this program work for the Indian system. In the Indian system. We have once 10s hundreds thousands then 10 thousands then lakhs then 10 lakhs and crores then 10 crores and so on. So, let us have a look at the changed code and see how it works. So, we are back in our studio, we have our function numbers towards I see that there is a change in the helper function.

So, here if we have one digit numbers we call the vector. However, if you have two digit number we call a function similarly, we have a separate function for three digital thousands and etc. So, you see the code for two digit numbers I have attached the entire code along with the lecture So you can read the code from the file which I provided you see that behind the scenes etc separately. Also we handle a trailing zero in case there is a number like 20 3040, etc The function of converting three digit number is handled you might get numbers like 1020 so there will be no hundred. So we have to handle that case as well. So similarly we have functions which you can see in the code which are attached.

Now, the remaining code is the same except we have removed suffix from this code. we compile the code now. See that it is available for use at this point of time to the console We try different numbers. See that it is working. We try with different numbers. You can try this code you will not find any flaws in this code.

Also, this code can handle any number of digits in a number Because this code uses recursion, and so when the number becomes more than 99.9999 rows, it will recurse and create another array for the larger numbers. See that this code is working quite nicely Now we will provide two numbers as input to this function number two words. Let's see what it does. So, you see it provides two outputs. And both outputs correspond to the numbers which we have provided as input. We can give more numbers of numbers also, we try with three numbers.

So here you see it works fine. So, we went through the process of writing the program using our own algorithm, but our provides you this feature through a library library called English. Using this library, we can very easily convert numbers to words. So to program it, we need to first install the library if it is not already installed, then we have to invoke the library English and then just use the function English and give the number for which you want the numbers to words. Let's try this on our studio. So here we are on our studio.

So you have planted the code that I've discussed with you before. So first, we have to see if the library is installed. If it is not installed, we installed the library. The most important thing is we should invoke the library before we can use the function to convert numbers to words, just clear the environment. Let's see if the library is installed. If not, we'll install it and then We will invoke the library.

So we see that the library is getting installed. Now it is installed. So now we can run the function English and see if it converts or numbers to words. So we see that it does it. Remember that this follows a European standard to the American Standard where the comma is placed after every three digits, so it always counts in thousands. So let's try one more number.

Gloves. So now you must remember that if you want it in any other convention like the idiot convention, this function will not help you you have to write your own function. Let's try it last number is one one and see if it works or not. It does That concludes our discussion. Thank you for listening

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.