Number to string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theroflchopper
    New Member
    • Oct 2009
    • 5

    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
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    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','t wo','three',... ,'nine']. Then units[n] would give you the string.

    Then you'd need to make a similar string for the teens=['eleven','twelv e',...'nineteen ']
    A similar one for the tens=['','ten','twent y',...,'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!!

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      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:
      Code:
          numDict1 = {0: '', 1:'one', 2:'two', 3:'three', 4:'four', 5:'five', \
                      6:'six', 7:'seven', 8:'eight', 9:'nine', 10:'ten', \
                      11:'eleven', 12:'twelve', 13:'thirteen', 14:'fourteen', \
                      15:'fifteen', 16:'sixteen', 17:'seventeen', 18:'eighteen', \
                      19:'nineteen', 20:'twenty', 30:'thirty', 40:'forty', \
                      50:'fifty', 60:'sixty', 70:'seventy', 80:'eighty', 90:'ninety'
                      }
          thousands, rem = divmod(num, 1000)
          hundreds, rem = divmod(rem, 100)
          tens, ones = divmod(rem, 10)

      Comment

      • theroflchopper
        New Member
        • Oct 2009
        • 5

        #4
        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.

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          I would like for you to show some effort to code this for yourself. Here's a hint:
          Code:
          numDict1 = {0: '', 1:'one', 2:'two', 3:'three', 4:'four', 5:'five', \
                      6:'six', 7:'seven', 8:'eight', 9:'nine', 10:'ten', \
                      11:'eleven', 12:'twelve', 13:'thirteen', 14:'fourteen', \
                      15:'fifteen', 16:'sixteen', 17:'seventeen', 18:'eighteen', \
                      19:'nineteen', 20:'twenty', 30:'thirty', 40:'forty', \
                      50:'fifty', 60:'sixty', 70:'seventy', 80:'eighty', 90:'ninety'
                      }
          
          num = 9999
          
          thousands, rem = divmod(num, 1000)
          hundreds, rem = divmod(rem, 100)
          tens, ones = divmod(rem, 10)
          
          print numDict1[thousands], "thousand"
          The output:
          >>> nine thousand

          Comment

          • theroflchopper
            New Member
            • Oct 2009
            • 5

            #6
            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.

            Code:
            while True:
                import time
                print "Testing word calculator v.1"
                time.sleep(1) 
                S = input("Enter first number to be added: ")
                S2 = input("Enter second number to be added: ")
                num = S + S2
                numDict1 = {0: '', 1:'one', 2:'two', 3:'three', 4:'four', 5:'five', \
                6:'six', 7:'seven', 8:'eight', 9:'nine', 10:'ten', \
                11:'eleven', 12:'twelve', 13:'thirteen', 14:'fourteen', \
                15:'fifteen', 16:'sixteen', 17:'seventeen', 18:'eighteen', \
                19:'nineteen', 20:'twenty', 30:'thirty', 40:'forty', \
                50:'fifty', 60:'sixty', 70:'seventy', 80:'eighty', 90:'ninety'
                }
                thousands, rem = divmod(num, 1000)
                hundreds, rem = divmod(rem, 100)
                tens, ones = divmod(rem, 10)
            
                print "Your number is " + numDict1[num]
            But thanks for the advice

            Comment

            • Glenton
              Recognized Expert Contributor
              • Nov 2008
              • 391

              #7
              You're basically there!
              You just need to build up your final number:
              Code:
              output=""
              if thousands>0:
                  output+=numDict1[thousands]+" thousand "
              if hundreds>0:
                  output+=numDict1[hundreds]+" hundred "
              if tens>1:
                  output+=numDict1[10*tens]+" "
                  if ones>0:
                      output+=numDict1[ones]
              else:
                  output+=numDict1[10*tens+ones]
              
              output=output.strip()  #to get rid of trailing spaces that may be left
              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!

              Comment

              Working...