Click here to Skip to main content
15,892,480 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: Difficult to sort : SOLVED!! Pin
PhilLenoir12-Feb-15 3:15
professionalPhilLenoir12-Feb-15 3:15 
GeneralRe: Difficult to sort Pin
BillWoodruff12-Feb-15 3:42
professionalBillWoodruff12-Feb-15 3:42 
GeneralRe: Difficult to sort Pin
Amarnath S12-Feb-15 3:46
professionalAmarnath S12-Feb-15 3:46 
GeneralRe: Difficult to sort Pin
BillWoodruff12-Feb-15 4:01
professionalBillWoodruff12-Feb-15 4:01 
GeneralRe: Difficult to sort Pin
#realJSOP12-Feb-15 4:29
mve#realJSOP12-Feb-15 4:29 
GeneralRe: Difficult to sort Pin
Kenneth Haugland12-Feb-15 6:30
mvaKenneth Haugland12-Feb-15 6:30 
AnswerRe: Difficult to sort Pin
Mario Majčica12-Feb-15 4:07
professionalMario Majčica12-Feb-15 4:07 
GeneralRe: Difficult to sort Pin
#realJSOP12-Feb-15 4:22
mve#realJSOP12-Feb-15 4:22 
It's not that difficult. Be a programmer fer crap sake:

C#
public class IntList : List<decimal>
    {
        public decimal NumericValue
        {
            get
            {
                DoMath(100);
                DoMath(1000);
                DoMath(1000000);
                DoMath(1000000000); 
                DoMath(1000000000000);
                decimal value = 0;
                foreach(decimal item in this)
                {
                    value += item;
                }
                return value;
            }
        }

        private void DoMath(decimal value)
        {
            int index = this.IndexOf(value);
            if (index >= 1)
            {
                value *= this[index-1];
                this[index] = value;
                this[index - 1] = 0;
            }
        }

        public int IndexOf(decimal value)
        {
            int index = -1;
            for (int i = 0; i < this.Count; i++)
            {
                if (this[i] == value)
                {
                    index = i;
                    break;
                }
            }
            return index;
        }
    }

    public static class NumberTranslator
    {
        private static IntList values;
        public static Dictionary<string, decimal> numbers = new Dictionary<string, decimal>()
        {
            {"ZERO", 0},
            {"FIRST", 1},
            {"ONE", 1},
            {"SECOND", 2},
            {"TWO", 2},
            {"THIRD", 3},
            {"THREE", 3},
            {"FOUR", 4},
            {"FIF", 5},
            {"FIVE", 5},
            {"SIX", 6},
            {"SEVEN", 7},
            {"EIGH", 8},
            {"NINE", 9},
            {"TEN", 10},
            {"ELEVEN", 11},
            {"TWELF", 12},
            {"TWELVE", 12},
            {"THIRTEEN", 13},
            {"FOURTEEN", 14},
            {"FIFTEEN", 15},
            {"SIXTEEN", 16},
            {"SEVENTEEN", 17},
            {"EIGHTEEN", 18},
            {"NINETEEN", 19},
            {"TWENTY", 20},
            {"THIRTY", 30},
            {"FOURTY", 40},
            {"FIFTY", 50},
            {"SIXTY", 60},
            {"SEVENTY", 70},
            {"EIGHTY", 80},
            {"NINETY", 90},
            {"HUNDRED", 100},
            {"THOUSAND", 1000},
            {"MILLION", 1000000},
            {"BILLION", 1000000000}
        };

        public static decimal Translate(string text)
        {
            text = text.ToUpper().Trim();
            string trimChars = "TH";
            text = text.Replace("TY", "TY ");
            text = text.Replace("-"," ").Replace("_"," ").Replace("."," ").Replace(",", " ");
            if (text.EndsWith(trimChars))
            {
                text = text.TrimEnd(trimChars.ToArray());
            }
            text = text.Replace("  ", " ");

            values = new IntList();
            string[] parts = text.Split(' ');
            foreach (string numberText in parts)
            {

                if (numbers.Keys.Contains(numberText))
                {
                    values.Add(numbers[numberText]);
                }
                else
                {
                    throw new Exception("Not a number (might be spelled wrong)");
                }
            }
            return values.NumericValue;
        }


Usage:

C#
decimal value = 0;
value = NumberTranslator.Translate("twelfth");
value = NumberTranslator.Translate("First");
value = NumberTranslator.Translate("One Hundredth");
value = NumberTranslator.Translate("five thousand seven Hundred thirtysecond");


I think that's a fairly complete solution.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013


JokeRe: Difficult to sort Pin
Bergholt Stuttley Johnson12-Feb-15 4:54
professionalBergholt Stuttley Johnson12-Feb-15 4:54 
GeneralRe: Difficult to sort Pin
#realJSOP12-Feb-15 5:44
mve#realJSOP12-Feb-15 5:44 
GeneralRe: Difficult to sort Pin
Bergholt Stuttley Johnson12-Feb-15 21:06
professionalBergholt Stuttley Johnson12-Feb-15 21:06 
GeneralRe: Difficult to sort Pin
BillWoodruff12-Feb-15 22:55
professionalBillWoodruff12-Feb-15 22:55 
GeneralRe: Difficult to sort - REAL SOLUTION Pin
#realJSOP12-Feb-15 9:25
mve#realJSOP12-Feb-15 9:25 
GeneralMQOTD Pin
V.11-Feb-15 21:36
professionalV.11-Feb-15 21:36 
GeneralRe: MQOTD Pin
Mendor8111-Feb-15 21:37
professionalMendor8111-Feb-15 21:37 
GeneralRe: MQOTD Pin
HobbyProggy11-Feb-15 21:41
professionalHobbyProggy11-Feb-15 21:41 
GeneralRe: MQOTD Pin
User 1013254611-Feb-15 21:48
User 1013254611-Feb-15 21:48 
GeneralRe: MQOTD Pin
Kenneth Haugland11-Feb-15 22:04
mvaKenneth Haugland11-Feb-15 22:04 
GeneralRe: MQOTD Pin
V.11-Feb-15 22:16
professionalV.11-Feb-15 22:16 
GeneralRe: MQOTD Pin
Kenneth Haugland11-Feb-15 22:24
mvaKenneth Haugland11-Feb-15 22:24 
GeneralRe: MQOTD Pin
Nagy Vilmos11-Feb-15 22:12
professionalNagy Vilmos11-Feb-15 22:12 
GeneralOT: CCC Pin
Agent__00711-Feb-15 22:50
professionalAgent__00711-Feb-15 22:50 
GeneralRe: OT: CCC Pin
Nagy Vilmos11-Feb-15 22:57
professionalNagy Vilmos11-Feb-15 22:57 
GeneralRe: MQOTD Pin
chriselst11-Feb-15 22:19
professionalchriselst11-Feb-15 22:19 
GeneralRe: MQOTD Pin
Johnny J.11-Feb-15 22:31
professionalJohnny J.11-Feb-15 22:31 

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.