473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Number to string

How would I change the output of my python program from being a number to being a string. In the simplest and best way

For example: instead of 1 it being "one"

This is probably a stupid question but I can't think of anything
Oct 7 '09 #1
6 3201
Glenton
391 Expert 256MB
Hi

I think you'd have to do this the hard way. Although maybe there's a module or piece of code out there somewhere that's done it already. Probably a good idea to make a class which has the various conversions.

You'd need to make a couple of lists, and pull them out. E.g. units=['zero','one','two','three',...,'nine']. Then units[n] would give you the string.

Then you'd need to make a similar string for the teens=['eleven','twelve',...'nineteen']
A similar one for the tens=['','ten','twenty',...,'ninety'].

Then you'd have to split your number into bits (e.g. make it a string, slice it and convert the slices back to numbers), and use a serious number of if statements to put the text number together. You'll need to set an upper bound on this!!
Oct 7 '09 #2
bvdet
2,851 Expert Mod 2GB
Good advice Glenton. Let's say you put the upper limit at 9999. Without showing you a complete solution, you might set up your output like this:
Expand|Select|Wrap|Line Numbers
  1.     numDict1 = {0: '', 1:'one', 2:'two', 3:'three', 4:'four', 5:'five', \
  2.                 6:'six', 7:'seven', 8:'eight', 9:'nine', 10:'ten', \
  3.                 11:'eleven', 12:'twelve', 13:'thirteen', 14:'fourteen', \
  4.                 15:'fifteen', 16:'sixteen', 17:'seventeen', 18:'eighteen', \
  5.                 19:'nineteen', 20:'twenty', 30:'thirty', 40:'forty', \
  6.                 50:'fifty', 60:'sixty', 70:'seventy', 80:'eighty', 90:'ninety'
  7.                 }
  8.     thousands, rem = divmod(num, 1000)
  9.     hundreds, rem = divmod(rem, 100)
  10.     tens, ones = divmod(rem, 10)
Oct 7 '09 #3
Ok but lets say I want to make a simple calculator that adds two numbers and outputs the word. I'm not sure how to incorporate the above into the output.
Oct 7 '09 #4
bvdet
2,851 Expert Mod 2GB
I would like for you to show some effort to code this for yourself. Here's a hint:
Expand|Select|Wrap|Line Numbers
  1. numDict1 = {0: '', 1:'one', 2:'two', 3:'three', 4:'four', 5:'five', \
  2.             6:'six', 7:'seven', 8:'eight', 9:'nine', 10:'ten', \
  3.             11:'eleven', 12:'twelve', 13:'thirteen', 14:'fourteen', \
  4.             15:'fifteen', 16:'sixteen', 17:'seventeen', 18:'eighteen', \
  5.             19:'nineteen', 20:'twenty', 30:'thirty', 40:'forty', \
  6.             50:'fifty', 60:'sixty', 70:'seventy', 80:'eighty', 90:'ninety'
  7.             }
  8.  
  9. num = 9999
  10.  
  11. thousands, rem = divmod(num, 1000)
  12. hundreds, rem = divmod(rem, 100)
  13. tens, ones = divmod(rem, 10)
  14.  
  15. print numDict1[thousands], "thousand"
The output:
>>> nine thousand
Oct 7 '09 #5
Yeah sor I haven't review python quite enough. I got it to work but it won't go over 21. But thanks I think I could figure it out myself now.

Expand|Select|Wrap|Line Numbers
  1. while True:
  2.     import time
  3.     print "Testing word calculator v.1"
  4.     time.sleep(1) 
  5.     S = input("Enter first number to be added: ")
  6.     S2 = input("Enter second number to be added: ")
  7.     num = S + S2
  8.     numDict1 = {0: '', 1:'one', 2:'two', 3:'three', 4:'four', 5:'five', \
  9.     6:'six', 7:'seven', 8:'eight', 9:'nine', 10:'ten', \
  10.     11:'eleven', 12:'twelve', 13:'thirteen', 14:'fourteen', \
  11.     15:'fifteen', 16:'sixteen', 17:'seventeen', 18:'eighteen', \
  12.     19:'nineteen', 20:'twenty', 30:'thirty', 40:'forty', \
  13.     50:'fifty', 60:'sixty', 70:'seventy', 80:'eighty', 90:'ninety'
  14.     }
  15.     thousands, rem = divmod(num, 1000)
  16.     hundreds, rem = divmod(rem, 100)
  17.     tens, ones = divmod(rem, 10)
  18.  
  19.     print "Your number is " + numDict1[num]
But thanks for the advice
Oct 7 '09 #6
Glenton
391 Expert 256MB
You're basically there!
You just need to build up your final number:
Expand|Select|Wrap|Line Numbers
  1. output=""
  2. if thousands>0:
  3.     output+=numDict1[thousands]+" thousand "
  4. if hundreds>0:
  5.     output+=numDict1[hundreds]+" hundred "
  6. if tens>1:
  7.     output+=numDict1[10*tens]+" "
  8.     if ones>0:
  9.         output+=numDict1[ones]
  10. else:
  11.     output+=numDict1[10*tens+ones]
  12.  
  13. output=output.strip()  #to get rid of trailing spaces that may be left
  14.  
Note this gives the American way of saying numbers, which drops the "and" after hundred ("four hundred and seventeen" vs "four hundred seventeen"). If you want it the other way, you'll just have to fiddle a bit!
Oct 8 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number...
7
by: netclectic | last post by:
Hi folks, i've searched and searched and can't find any example of what i'm trying to do. Essentially (i think) i need to add a new operator to Number. I'm using eval to evaluate expressions...
25
by: Jason | last post by:
Hi, below is example code which demonstrates a problem I have encountered. When passing a number to a function I compare it with a string's size and then take certain actions, unfortunately during...
4
by: B. Fletcher | last post by:
Hi, I'm having some trouble with javascript code of mine: When the script runs, I vget an error in line 119: Number Expected. I'm not sure as to why this is happening. Any advice would be...
23
by: Davey | last post by:
How do I display an integer in binary format in C? e.g. 4 displayed as "100"
1
by: Andrew Arace | last post by:
I searched for something like this existing already, failing to find it, I wrote it myself. If this already existed somewhere in the framework I appologize for my ignorance. This method will...
6
by: Jovo Mirkovic | last post by:
Hi, I have to make a program which will generate 100,000 different ID numbers (for example 2345-9341-0903-3432-3432 ...) It must be really different, I meen it can not be a similar (like...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.