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

    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
        • M Member_5893260

          So I've been enjoying myself at work today: I wrote something to take a number and convert it into English, e.g. "12324.56" becomes "twelve thousand, three hundred and twenty-four point five six". I know this is an old problem: I remember having done this something like 30 years ago, as part of a computer science class, but I actually needed it for something today: in doing it, I was amazed at how many ways there are to achieve it in C# (the last time I wrote it, it was in Algol-60!), and how many little optimizations I was able to add as I sat there looking at each iteration of the code. I ended up trying to keep the code as terse as I could but also as fast as I could, without having too many IFs and things all over the place. I ended up using a bunch of enums and letting the runtime make words out of them, rather than having strings for it: I'm not sure it makes a huge difference, but it just seemed more elegant, somehow. Of course, in a problem like this, there's always the part about trying to stop it saying things like, "two thousand, zero hundred and onety-zero", so part of the fun was trying not to write anything too specific to avoid things like that: in my mind, if I got the algorithm right, that stuff would just sort of work... It's nice having a bit of time on one's hands at work, for a change. Anyway, I had a lot of fun, so I thought I'd share: if anyone else has a better method (and I'm sure they do) then why not join in...? Meanwhile, here's my version:

          public static class Numeric
          {
          private enum Digit
          {
          zero = 0, one = 1, two = 2, three = 3, four = 4,
          five = 5, six = 6, seven = 7, eight = 8, nine = 9
          }

          private enum Teen
          {
          ten = 10, eleven = 11, twelve = 12, thirteen = 13, fourteen = 14,
          fifteen = 15, sixteen = 16, seventeen = 17, eighteen = 18, nineteen = 19
          }

          private enum Ten
          {
          twenty = 2, thirty = 3, forty = 4, fifty = 5,
          sixty = 6, seventy = 7, eighty = 8, ninety = 9
          }

          private enum PowerOfTen
          {
          hundred = 0, thousand = 1, million = 2, billion = 3,
          trillion = 4, quadrillion = 5, quintillion = 6
          }

          /// /// How many powers of ten there are; faster to work this out ahead of time,
          /// and I didn't want to hard-code it into the algorithm...
          ///
          private static int PowersOfTen = Enum.GetValues(typeof(PowerOfTen)).Length;

          /// /// Converts a number to English words
          ///
          /// The number

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

          Thanks for a few days' exercise. I made one that can be used like: 2345L.ToString<ShortScale>() ==> Two Thousand Three Hundred Forty-Five Where ShortScale is:

          public enum ShortScale : long
          {
          Zero = 0 , One = 1 , Two = 2 , Three = 3 , Four = 4 , Five = 5 , Six = 6 , Seven = 7 , Eight = 8
          , Nine = 9 , Ten = 10 , Eleven = 11 , Twelve = 12 , Thirteen = 13 , Fourteen = 14 , Fifteen = 15
          , Sixteen = 16 , Seventeen = 17 , Eighteen = 18 , Nineteen = 19

          , \[PIEBALD.Types.NumNuts.HyphenatedAttribute\] Twenty   = 20  
          

          ...
          , [PIEBALD.Types.NumNuts.CountedAttribute] Hundred = 100
          ...
          }

          I also have an enum that will yield Twenty-Three Hundred Forty-Five, but it needs work. Here's another: 2345L.ToString<Roman>() ==> MMCCCXLV

          public enum Roman
          {
          Nulla = 0
          , [PIEBALD.Types.NumNuts.RepeatedAttribute] I = 1
          , [PIEBALD.Types.NumNuts.RepeatedAttribute] IV = 4
          , [PIEBALD.Types.NumNuts.RepeatedAttribute] V = 5
          , [PIEBALD.Types.NumNuts.RepeatedAttribute] IX = 9
          , [PIEBALD.Types.NumNuts.RepeatedAttribute] X = 10
          ...
          }

          But this technique tops out at Quintillion (short scale) or Trillion (long scale), so another technique would be necessary to work with greater numbers. It should be possible to define a class of BigInteger constants and use that instead. Added:

          public sealed class TestClass
          {
          public static readonly System.Numerics.BigInteger Zero = 0 ;
          public static readonly System.Numerics.BigInteger One = 1 ;
          ...
          [PIEBALD.Types.NumNuts.HyphenatedAttribute]
          public static readonly System.Numerics.BigInteger Twenty = 20 ;
          ...
          [PIEBALD.Types.NumNuts.CountedAttribute]
          public static readonly System.Numerics.BigInteger Hundred = 100 ;
          ...
          [PIEBALD.Types.NumNuts.CountedAttribute]
          public static readonly System.Numerics.BigInteger Decillion = Thousand * Nonillion ;
          ...
          }

          M 1 Reply Last reply
          0
          • R R Giskard Reventlov

            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 Offline
            L Offline
            Lost User
            wrote on last edited by
            #46

            mark merrens wrote:

            No sir,

            Nice of you to call me Sir, anyway.

            mark merrens wrote:

            would suggest purchasing a sense of humor: you plainly lack one

            So you calling me a pompous ass is funny but me saying "fuck you" isn't? And you want me to get a sense of humour?

            mark merrens wrote:

            so far up your own ass

            I honestly don't see why you are having a go at me at all! The OP said "at work today: I wrote something to take a number and convert it into English" I asked "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'?" which I feel is a fair and reasonable set of questions. Later the OP pointed out that they are self employed and were writing for fun - which is fair enough and negates my questions. In what way, then, is that pompous? In what manner does that show me to be up my own ass?

            mark merrens wrote:

            Have a nice day, twat.

            I have reported this post as abusive, as it clearly is. feel free to report my 'f u' post as abusive if, for some reason, my short insult post cannot be amusing while yours can. In the meantime, may I suggest you look around for a cheap, second hand sense of humour, and take a large chill pill?

            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
            • L Lost User

              mark merrens wrote:

              No sir,

              Nice of you to call me Sir, anyway.

              mark merrens wrote:

              would suggest purchasing a sense of humor: you plainly lack one

              So you calling me a pompous ass is funny but me saying "fuck you" isn't? And you want me to get a sense of humour?

              mark merrens wrote:

              so far up your own ass

              I honestly don't see why you are having a go at me at all! The OP said "at work today: I wrote something to take a number and convert it into English" I asked "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'?" which I feel is a fair and reasonable set of questions. Later the OP pointed out that they are self employed and were writing for fun - which is fair enough and negates my questions. In what way, then, is that pompous? In what manner does that show me to be up my own ass?

              mark merrens wrote:

              Have a nice day, twat.

              I have reported this post as abusive, as it clearly is. feel free to report my 'f u' post as abusive if, for some reason, my short insult post cannot be amusing while yours can. In the meantime, may I suggest you look around for a cheap, second hand sense of humour, and take a large chill pill?

              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
              #47

              I won't be so childish as to report you and I didn't say fuck you: that was your game. So, again, feel free to do one.

              "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

              1 Reply Last reply
              0
              • P PIEBALDconsult

                Thanks for a few days' exercise. I made one that can be used like: 2345L.ToString<ShortScale>() ==> Two Thousand Three Hundred Forty-Five Where ShortScale is:

                public enum ShortScale : long
                {
                Zero = 0 , One = 1 , Two = 2 , Three = 3 , Four = 4 , Five = 5 , Six = 6 , Seven = 7 , Eight = 8
                , Nine = 9 , Ten = 10 , Eleven = 11 , Twelve = 12 , Thirteen = 13 , Fourteen = 14 , Fifteen = 15
                , Sixteen = 16 , Seventeen = 17 , Eighteen = 18 , Nineteen = 19

                , \[PIEBALD.Types.NumNuts.HyphenatedAttribute\] Twenty   = 20  
                

                ...
                , [PIEBALD.Types.NumNuts.CountedAttribute] Hundred = 100
                ...
                }

                I also have an enum that will yield Twenty-Three Hundred Forty-Five, but it needs work. Here's another: 2345L.ToString<Roman>() ==> MMCCCXLV

                public enum Roman
                {
                Nulla = 0
                , [PIEBALD.Types.NumNuts.RepeatedAttribute] I = 1
                , [PIEBALD.Types.NumNuts.RepeatedAttribute] IV = 4
                , [PIEBALD.Types.NumNuts.RepeatedAttribute] V = 5
                , [PIEBALD.Types.NumNuts.RepeatedAttribute] IX = 9
                , [PIEBALD.Types.NumNuts.RepeatedAttribute] X = 10
                ...
                }

                But this technique tops out at Quintillion (short scale) or Trillion (long scale), so another technique would be necessary to work with greater numbers. It should be possible to define a class of BigInteger constants and use that instead. Added:

                public sealed class TestClass
                {
                public static readonly System.Numerics.BigInteger Zero = 0 ;
                public static readonly System.Numerics.BigInteger One = 1 ;
                ...
                [PIEBALD.Types.NumNuts.HyphenatedAttribute]
                public static readonly System.Numerics.BigInteger Twenty = 20 ;
                ...
                [PIEBALD.Types.NumNuts.CountedAttribute]
                public static readonly System.Numerics.BigInteger Hundred = 100 ;
                ...
                [PIEBALD.Types.NumNuts.CountedAttribute]
                public static readonly System.Numerics.BigInteger Decillion = Thousand * Nonillion ;
                ...
                }

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

                That's really interesting... I completely didn't think about doing it like that...

                P 2 Replies Last reply
                0
                • M Member_5893260

                  That's really interesting... I completely didn't think about doing it like that...

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

                  Neither had I.

                  1 Reply Last reply
                  0
                  • M Member_5893260

                    That's really interesting... I completely didn't think about doing it like that...

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

                    Oh, and I finally located this: Converting numbers to the word equivalent. [^]

                    M 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Oh, and I finally located this: Converting numbers to the word equivalent. [^]

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

                      Brilliant! That put a grin on my face this morning!

                      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