Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Converting numbers into English

Converting numbers into English

Scheduled Pinned Locked Moved The Lounge
csharpalgorithmshelpquestionannouncement
51 Posts 11 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    What I would ask myself is: Why did you re-invent the wheel? Was this good value for money for your employer? There are many many solutions available to do this (for free) on the interwebs. Why did it need to be 'terse and fast' - surely better to be 'easily readable, debuggable and maintainable'?

    MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

    M Offline
    M Offline
    Marc A Brown
    wrote on last edited by
    #25

    1. Why not? 2. To learn the language. Beats the heck out of "Hello World". 3. For the same reason people draw doodles. 4. Why not? :)

    M L 2 Replies Last reply
    0
    • N NormDroid

      How about to roman numerals ;)

      Software Kinetics - Dependable Software news

      M Offline
      M Offline
      Member_5893260
      wrote on last edited by
      #26

      Oh, yeah - that's fun - actually quite difficult!

      P 1 Reply Last reply
      0
      • M Mark_Wallace

        Oh oh, that's not how you do phone numbers.

        I wanna be a eunuchs developer! Pass me a bread knife!

        M Offline
        M Offline
        Member_5893260
        wrote on last edited by
        #27

        ROFL!

        1 Reply Last reply
        0
        • L Lost User

          You had to go no further than our very own CP[^] or I stole this from StuckOverflow...

          public static string NumberToWords(int number)
          {
          if (number == 0)
          return "zero";

          if (number < 0)
              return "minus " + NumberToWords(Math.Abs(number));
          
          string words = "";
          
          if ((number / 1000000) > 0)
          {
              words += NumberToWords(number / 1000000) + " million ";
              number %= 1000000;
          }
          
          if ((number / 1000) > 0)
          {
              words += NumberToWords(number / 1000) + " thousand ";
              number %= 1000;
          }
          
          if ((number / 100) > 0)
          {
              words += NumberToWords(number / 100) + " hundred ";
              number %= 100;
          }
          
          if (number > 0)
          {
              if (words != "")
                  words += "and ";
          
              var unitsMap = new\[\] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
              var tensMap = new\[\] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
          
              if (number < 20)
                  words += unitsMap\[number\];
              else
              {
                  words += tensMap\[number / 10\];
                  if ((number % 10) > 0)
                      words += "-" + unitsMap\[number % 10\];
              }
          }
          
          return words;
          

          }

          MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

          M Offline
          M Offline
          Member_5893260
          wrote on last edited by
          #28

          Yeah - I thought about doing it that way but it wasn't fun to write! I'm sure I overcomplicated it, but - whatever - I had fun!

          1 Reply Last reply
          0
          • M Marc A Brown

            1. Why not? 2. To learn the language. Beats the heck out of "Hello World". 3. For the same reason people draw doodles. 4. Why not? :)

            M Offline
            M Offline
            Member_5893260
            wrote on last edited by
            #29

            That's it - it's a doodle!

            1 Reply Last reply
            0
            • L Lost User

              mark merrens wrote:

              Curmudgeonprofessional

              ftfy

              MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

              R Offline
              R Offline
              R Giskard Reventlov
              wrote on last edited by
              #30

              _Maxxx_ wrote:

              Curmudgeonprofessional

              Pompous ass. That should do it.

              "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. Those who seek perfection will only find imperfection nils illegitimus carborundum me, me, me me, in pictures

              M L 2 Replies Last reply
              0
              • M Member_5893260

                Really? Convert's slow? Thanks!

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #31

                Dan Sutton wrote:

                Really? Convert's slow? Thanks!

                Good luck ever finding a real application where that has even a measurable impact on performance though.

                1 Reply Last reply
                0
                • M Member_5893260

                  A parser isn't really applicable here: you can't exactly parse a string of digits into English...

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #32

                  Convert.ToInt32(N % p); Use a cast. There is only one useful method in Convert -- ChangeType. For all other uses for Convert, there are better alternatives.

                  M 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Convert.ToInt32(N % p); Use a cast. There is only one useful method in Convert -- ChangeType. For all other uses for Convert, there are better alternatives.

                    M Offline
                    M Offline
                    Member_5893260
                    wrote on last edited by
                    #33

                    Noted - very good.

                    1 Reply Last reply
                    0
                    • R R Giskard Reventlov

                      _Maxxx_ wrote:

                      Curmudgeonprofessional

                      Pompous ass. That should do it.

                      "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. Those who seek perfection will only find imperfection nils illegitimus carborundum me, me, me me, in pictures

                      M Offline
                      M Offline
                      Member_5893260
                      wrote on last edited by
                      #34

                      Concur!

                      1 Reply Last reply
                      0
                      • L Lost User

                        You had to go no further than our very own CP[^] or I stole this from StuckOverflow...

                        public static string NumberToWords(int number)
                        {
                        if (number == 0)
                        return "zero";

                        if (number < 0)
                            return "minus " + NumberToWords(Math.Abs(number));
                        
                        string words = "";
                        
                        if ((number / 1000000) > 0)
                        {
                            words += NumberToWords(number / 1000000) + " million ";
                            number %= 1000000;
                        }
                        
                        if ((number / 1000) > 0)
                        {
                            words += NumberToWords(number / 1000) + " thousand ";
                            number %= 1000;
                        }
                        
                        if ((number / 100) > 0)
                        {
                            words += NumberToWords(number / 100) + " hundred ";
                            number %= 100;
                        }
                        
                        if (number > 0)
                        {
                            if (words != "")
                                words += "and ";
                        
                            var unitsMap = new\[\] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
                            var tensMap = new\[\] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
                        
                            if (number < 20)
                                words += unitsMap\[number\];
                            else
                            {
                                words += tensMap\[number / 10\];
                                if ((number % 10) > 0)
                                    words += "-" + unitsMap\[number % 10\];
                            }
                        }
                        
                        return words;
                        

                        }

                        MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #35

                        Not very flexible.

                        L 1 Reply Last reply
                        0
                        • N NormDroid

                          How about to roman numerals ;)

                          Software Kinetics - Dependable Software news

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #36

                          I added that to my version last night. And today I wondered about adding support for doing thousands as hundreds, e.g. "twenty-five hundred".

                          1 Reply Last reply
                          0
                          • M Mark_Wallace

                            Oh oh, that's not how you do phone numbers.

                            I wanna be a eunuchs developer! Pass me a bread knife!

                            P Offline
                            P Offline
                            PIEBALDconsult
                            wrote on last edited by
                            #37

                            Phone numbers aren't numbers.

                            1 Reply Last reply
                            0
                            • M Member_5893260

                              Oh, yeah - that's fun - actually quite difficult!

                              P Offline
                              P Offline
                              PIEBALDconsult
                              wrote on last edited by
                              #38

                              Only as difficult as you like.

                              1 Reply Last reply
                              0
                              • P PIEBALDconsult

                                Not very flexible.

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #39

                                True -0 but the likelihood of the number system changing any time soon is low ;)

                                MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                                P 1 Reply Last reply
                                0
                                • R R Giskard Reventlov

                                  _Maxxx_ wrote:

                                  Curmudgeonprofessional

                                  Pompous ass. That should do it.

                                  "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. Those who seek perfection will only find imperfection nils illegitimus carborundum me, me, me me, in pictures

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #40

                                  Well fuck you

                                  MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                                  R 1 Reply Last reply
                                  0
                                  • M Marc A Brown

                                    1. Why not? 2. To learn the language. Beats the heck out of "Hello World". 3. For the same reason people draw doodles. 4. Why not? :)

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #41

                                    Marc A. Brown wrote:

                                    1. Why not?

                                    Well he said he did it at work, so I assumed it was something required for the business rather than being done as a leisure activity. So re-inventing the wheel is not generally a good thing for a business to do with so trivial a thing.

                                    Marc A. Brown wrote:

                                    To learn the language. Beats the heck out of "Hello World".

                                    Again - he was doing it at work & I assumed that he wasn't doing it for fun but for a business requirement.

                                    Marc A. Brown wrote:

                                    4. Why not? :)

                                    Because

                                    MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                                    P 1 Reply Last reply
                                    0
                                    • L Lost User

                                      True -0 but the likelihood of the number system changing any time soon is low ;)

                                      MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                                      P Offline
                                      P Offline
                                      PIEBALDconsult
                                      wrote on last edited by
                                      #42

                                      Think globally.

                                      1 Reply Last reply
                                      0
                                      • L Lost User

                                        Marc A. Brown wrote:

                                        1. Why not?

                                        Well he said he did it at work, so I assumed it was something required for the business rather than being done as a leisure activity. So re-inventing the wheel is not generally a good thing for a business to do with so trivial a thing.

                                        Marc A. Brown wrote:

                                        To learn the language. Beats the heck out of "Hello World".

                                        Again - he was doing it at work & I assumed that he wasn't doing it for fun but for a business requirement.

                                        Marc A. Brown wrote:

                                        4. Why not? :)

                                        Because

                                        MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                                        P Offline
                                        P Offline
                                        PIEBALDconsult
                                        wrote on last edited by
                                        #43

                                        _Maxxx_ wrote:

                                        he said he did it at work, so I assumed it was something required for the business

                                        I did most of my Turing Machine experiments at a previous job. :~

                                        1 Reply Last reply
                                        0
                                        • L Lost User

                                          Well fuck you

                                          MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                                          R Offline
                                          R Offline
                                          R Giskard Reventlov
                                          wrote on last edited by
                                          #44

                                          No sir, fuck you. I would suggest purchasing a sense of humor: you plainly lack one and are so far up your own ass I can't believe you can see well enough to have written these posts. Have a nice day, twat.

                                          "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. Those who seek perfection will only find imperfection nils illegitimus carborundum me, me, me me, in pictures

                                          L 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups