Click here to Skip to main content
15,902,766 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Quit the internet Pin
CBadger25-Sep-13 21:56
professionalCBadger25-Sep-13 21:56 
GeneralRe: Quit the internet Pin
Mark_Wallace25-Sep-13 21:32
Mark_Wallace25-Sep-13 21:32 
GeneralRe: Quit the internet Pin
CBadger25-Sep-13 21:55
professionalCBadger25-Sep-13 21:55 
GeneralRe: Quit the internet Pin
Argonia25-Sep-13 22:53
professionalArgonia25-Sep-13 22:53 
GeneralRe: Quit the internet Pin
CBadger25-Sep-13 22:56
professionalCBadger25-Sep-13 22:56 
GeneralRe: Quit the internet Pin
Mark_Wallace26-Sep-13 1:55
Mark_Wallace26-Sep-13 1:55 
GeneralRe: Quit the internet Pin
CBadger26-Sep-13 2:28
professionalCBadger26-Sep-13 2:28 
GeneralConverting numbers into English PinPopular
Dan Sutton25-Sep-13 13:38
Dan Sutton25-Sep-13 13:38 
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:

C#
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 
  }

  /// <summary>
  /// 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...
  /// </summary>
  private static int PowersOfTen = Enum.GetValues(typeof(PowerOfTen)).Length;

  /// <summary>
  /// Converts a number to English words
  /// </summary>
  /// <param name="N">The number</param>
  /// <returns>The number, in English</returns>
  public static string ToWords(double N)
  {
    string Prefix = N < 0 ? "minus " : "";
    string Significand = Digit.zero.ToString();
    string Mantissa = "";
    if ((N = Math.Abs(N)) > 0)
    {
      // Do the Mantissa
      if (N != Math.Floor(N))
      {
        Mantissa = " point";
        foreach (char C in N.ToString().Substring(N.ToString().IndexOf('.') + 1))
          Mantissa += " " + ((Digit)(int.Parse(C.ToString())));
      }

      // Figure out the bit of the Significand less than 100
      int n = Convert.ToInt32(N = Math.Floor(N)) % 100;
      Significand = n == 0 ? ""
        : n < 10 ? ((Digit)n).ToString()
        : n < 20 ? ((Teen)n).ToString()
        : (Ten)(n / 10) + "-" + (Digit)(n % 10);

      // Do the other powers of 10, if there are any
      if ((N = Math.Floor(N / 100D)) > 0)
      {
        string EW = "";
        for (int i = 0; (N > 0) && (i < PowersOfTen); i++)
        {
          double p = Math.Pow(10, (i << 1) + 1);
          n = Convert.ToInt32(N % p);
          if (n > 0)
            EW = ToWords(n) + " " + (PowerOfTen)i + (EW.Length == 0 ? "" : ", " + EW);
          N = Math.Floor(N / p);
        }
        if (EW.Length > 0)
          Significand = EW + (Significand.Length == 0 ? "" : " and " + Significand);
      }
    }
    return Prefix + (Significand + Mantissa).Trim();
  }
}

GeneralRe: Converting numbers into English Pin
_Damian S_25-Sep-13 13:43
professional_Damian S_25-Sep-13 13:43 
GeneralRe: Converting numbers into English Pin
Dan Sutton25-Sep-13 13:57
Dan Sutton25-Sep-13 13:57 
GeneralRe: Converting numbers into English Pin
PIEBALDconsult25-Sep-13 13:50
mvePIEBALDconsult25-Sep-13 13:50 
GeneralRe: Converting numbers into English Pin
Dan Sutton25-Sep-13 13:53
Dan Sutton25-Sep-13 13:53 
GeneralRe: Converting numbers into English Pin
PIEBALDconsult25-Sep-13 14:16
mvePIEBALDconsult25-Sep-13 14:16 
GeneralRe: Converting numbers into English Pin
Richard Andrew x6425-Sep-13 15:24
professionalRichard Andrew x6425-Sep-13 15:24 
GeneralRe: Converting numbers into English Pin
Dan Sutton25-Sep-13 15:58
Dan Sutton25-Sep-13 15:58 
GeneralRe: Converting numbers into English Pin
Richard Andrew x6425-Sep-13 16:00
professionalRichard Andrew x6425-Sep-13 16:00 
GeneralRe: Converting numbers into English Pin
Garth J Lancaster25-Sep-13 22:28
professionalGarth J Lancaster25-Sep-13 22:28 
GeneralRe: Converting numbers into English Pin
PIEBALDconsult25-Sep-13 16:14
mvePIEBALDconsult25-Sep-13 16:14 
GeneralRe: Converting numbers into English Pin
Dan Sutton25-Sep-13 19:55
Dan Sutton25-Sep-13 19:55 
GeneralRe: Converting numbers into English Pin
PIEBALDconsult26-Sep-13 13:23
mvePIEBALDconsult26-Sep-13 13:23 
GeneralRe: Converting numbers into English Pin
Dan Sutton26-Sep-13 13:40
Dan Sutton26-Sep-13 13:40 
GeneralRe: Converting numbers into English Pin
jschell26-Sep-13 9:01
jschell26-Sep-13 9:01 
GeneralRe: Converting numbers into English Pin
Dan Sutton25-Sep-13 13:55
Dan Sutton25-Sep-13 13:55 
GeneralRe: Converting numbers into English Pin
_Maxxx_25-Sep-13 14:03
professional_Maxxx_25-Sep-13 14:03 
GeneralRe: Converting numbers into English Pin
R. Giskard Reventlov25-Sep-13 14:48
R. Giskard Reventlov25-Sep-13 14:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.