2

I am looking for a c# function which converts amount in words to relative number.

For an example, one thousand twenty five should be converted to 1025.

Early help of anyone is appreciated.

leppie
  • 115,091
  • 17
  • 196
  • 297
Chirag Sheth
  • 47
  • 1
  • 2
  • 2
    Show us what have you tried... – Marco Feb 10 '12 at 10:06
  • You will find this answer useful: http://stackoverflow.com/a/1077651/104435 It's pseudocode so you'll have to convert it to C# on your own – soniiic Feb 10 '12 at 10:08
  • 1
    The question itself is interesting, though. Are there libraries for this? Which one to use? This is not something you should roll your own. – Daren Thomas Feb 10 '12 at 10:08
  • possible duplicate of [Converting words to numbers in PHP](http://stackoverflow.com/questions/1077600/converting-words-to-numbers-in-php) – ChrisF Feb 10 '12 at 10:08
  • possibly duplicate of http://stackoverflow.com/questions/493174/is-there-a-way-to-convert-number-words-to-integers-python – Hemant Metalia Feb 10 '12 at 10:13
  • possible duplicate of http://www.dotnetlogix.com/Codesnippet/Csharp/87/Conveting-Number-to-Word-in-C.html – vikiiii Feb 10 '12 at 10:15
  • There is a possibility to do it vice versa... http://midnightprogrammer.net/post/Convert-Numbers-to-Words-in-C.aspx I am eager to see if we can do it this way. – Scorpion Feb 10 '12 at 10:21

3 Answers3

5

Have tested it to work on strings containing multiple numbers.

Usage: findNumbers("The cost is five hundred twenty nine thousand three hundred and twenty six dollars and twenty three cents");

Output: "The cost is 529326 dollars and 23 cents"

    string numstring = "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;twenty=20;thirty=30;fourty=40;fifty=50;sixty=60;seventy=70;eighty=80;ninety=90;hundred=100;thousand=1000;";
    Dictionary<string, string> theNumbers = numstring.TrimEnd(';').Split(';').ToDictionary(item => item.Split('=')[0], item => item.Split('=')[1]);
    private string findNumbers(string input)
    {
        string tmp = "", tmpout = "", output = "";
        input = input.Replace("hundred and", "hundred");
        foreach (string word in input.Split(' '))
        {
            if (theNumbers.TryGetValue(word, out tmp))
            {
                if (tmpout != "") tmpout += " ";
                tmpout += tmp;
            } else
            {
                if (tmpout != "") output += " " + addNumbers(tmpout);
                tmpout = "";
                if (output != "") output += " ";
                output += word;
            }
        }
        if (tmpout != "") {
            tmpout = addNumbers(tmpout);
            if (output != "") output += " ";
            output += tmpout;
        }
        return output;
    }
    private string addNumbers(string input)
    {
        int output = 0;
        int output2 = 0;
        foreach (string num in input.Split(' '))
        {
            if (output > 999)
            {
                output2 = output;
                output = 0;
            }
            if (Int32.Parse(num) > 99)
            {
                output = output * Int32.Parse(num);
            } else
            {
                output = output + Int32.Parse(num);
            }
        }
        return (output + output2).ToString();
    }
derekantrican
  • 1,891
  • 3
  • 27
  • 57
Steve
  • 51
  • 1
  • 4
  • Note that this does not work for a string such as "five nine". The result would be expected to be "5 9" but the actual result is "14". It also assumes that the word "and" will only come after the word "hundred" so strings like "one thousand and five" result in "1000 and 5" which should actually be "1005" – derekantrican Oct 23 '19 at 17:24
1

Check the link bellow, its Written in Python but you can see the logic if its useful or not.

Regards

Is there a way to convert number words to Integers?

Community
  • 1
  • 1
Scorpion
  • 4,495
  • 7
  • 39
  • 60
0

There's a ruby gem by Marc Burns that does it. I recently forked it to add support for years. You can call ruby code from C#.

Community
  • 1
  • 1
dimid
  • 7,285
  • 1
  • 46
  • 85