Program for converting a number into Text in Python

Here is the most awaiting post for this week. After reading this post, you will be able to write a program using python to convert a given number into Text. This post is a small python tutorial for beginners.

Python is most widely used general-purpose, high-level programming language. Its design  emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java. Did you know, Search engine giant Google also is designed using python scripting. So, to demonstrate its features, let us see a small program to have an introduction to python.

Program for converting given number into Text in Python

Before jumping into the post :

Make sure that you are familiar with the following.

  1. A PC with Python installed. If you don't have Python you can download it from here. Python comes with all development tools for basic programming. https://www.python.org/downloads/.
  2. Basic knowledge on programming constructs in Python. Knowledge on how to write a python program and how to run program in python. Just refer this material for basics to learn how to program in python (for beginners). https://wiki.python.org/moin/BeginnersGuide.
Proceed to the content if you have the above requirements.

ALSO READ:Designing an Analog Clock in C programming - Clearly explained

Source Code of python program to convert numbers to words:

As we know the indentation of code matters a lot in python you might be little confused how this code can work, if you are from C/C++ background. Don't worry learn the basics. You'll feel better. I'm not going to explain the very basics in this program.
Coming to the first line of code that is to be executed is a Welcome message and then followed by the inputs from the user. Check the following code to learn how to input a number in python

print ("*****WELCOME TO THE PROGRAM TO CONVERT NUMBERS INTO CORRESPONDING SPELLINGS*****");

print(" \nEnter the number\n");
num=int(input());

temp=num;
fbasic(temp);
input();

I'll explain this code by taking an example.
Let the input to the program is 786453.
So, after getting the input num=786453
I'll store this in a temporary variable named temp and call a function named fbasic using temp as a formal parameter.

fbasic() is a function used to resolve the number into smaller bits i.e, if the number is less than 100 or lies between 99 to 999 etc. just go through the below code and you will know it how to resolve a number into small bits.

def fbasic(temp):
 if(temp<=20):
  word(temp);
 elif(temp>20 and temp<=99):
  fun1(temp);
 elif(temp>99 and temp<=999):
  fun2(temp);
 elif(temp>999 and temp<=99999):
  fun3(temp);
 elif(temp>99999 and temp<=9999999):
  fun4(temp);
 elif(temp>9999999 and temp<=999999999):
  fun5(temp);
 elif(temp>999999999):
  fun6(temp);

Our number is 786453 falls between 99999 and 999999. So the function to be called is fun4(temp).

fun4() is defined to send the first digit to the fbasic() and resolve it again. Its definition is as follows.

def fun5(x):
 fbasic(x //10000000);
 print(" crores ");
 fbasic(x%10000000);

So, in our case we send 7 to fbasic() . As 7<20, the control goes to word() function which prints the number in text by a laddered if else cases. Below is the code.

def word(cpy_num):
 if(cpy_num==1):
  print ("one");

 elif (cpy_num==2):
  print("two");

 elif(cpy_num==3):
  print("three");

 elif(cpy_num==4):
  print("four");

 elif(cpy_num==5):
  print("five");

 elif(cpy_num==6):
  print("six");

 elif(cpy_num==7):
  print("seven");

 elif(cpy_num==8):
  print("eight");

 elif(cpy_num==9):
  print("nine");

 elif(cpy_num==10):
  print("ten");

 elif(cpy_num==11):
  print("eleven");

 elif(cpy_num==12):
  print("twelve");

 elif(cpy_num==13):
  print("thirteen");

 elif(cpy_num==14) :
  print("fourteen");

 elif(cpy_num==15):
  print("fifteen");

 elif(cpy_num==16):
  print("sixteen");

 elif(cpy_num==17):
  print("seventeen");

 elif(cpy_num==18):
  print("eighteen");

 elif(cpy_num==19):
  print("nineteen");

 elif(cpy_num==20):
  print("twenty");
Then we print crores on the screen. For now the out put is 7 crores on the console. Proceeding in such a way would give the entire Text form of the given number. Here are the definitions for remaining functions.

def fun1(x):
 if(x//10==2 or x//10==3 or x //10==5):
   if(x //10==2):
     print("twenty ");
   if(x //10==3):
     print("thirty ");
   if(x //10==5):
     print("fifty ");
   word(x%10);
 else:
  word(x //10);
  if((x //10)==4 or(x //10)==6 or(x //10)==7 or(x //10)==9):
   print("ty ");
  if((x //10)==8):
   print("y ");
  word((x%10));

def fun2(x):
 word(x //100);
 if((x%100)!=0):
  print(" hundred and ");
 else:
  print(" hundred ");
 fbasic(x%100);

def fun3(x):
 fbasic(x //1000);
 print(" thousand ");
 fbasic(x%1000);

def fun4(x):
  fbasic(x //100000);
  print(" lakhs ");
  fbasic(x%100000);

def fun6(x):
 fbasic(x //10000000);
 print(" crores ");
 fbasic(x%10000000);

I've used a lot of functions to make it more modular and easy to understand. So, we can minimize a the code a lot. You can try if you like to minimize the code using arrays to store all the numbers spellings as strings.
You can see the sample out put here.

screenshot-of-program-for-converting-numbers-into-text

I've very little experience with python. The text is little messing up, you can see that every spelling is displayed in a different line. I'll sort it out and definitely release next version of this program making it as short as possible.


If you have any suggestions, Let me know by dropping a comment below.

0 comments:

Post a Comment

Please DO NOT SPAM. You're not allowed to use links in comments unless it's necessary.

I Appreciate your valuable Feedback.

Regards,

www.TricksStar.com