如何将字数转换为数字 - c#

我正在寻找一个c#函数,将单词数量转换为相对数字。

例如,应将一千二百五十转换为1025。

感谢任何人的早期帮助。

参考方案

经过测试,可以在包含多个数字的字符串上使用。

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

输出:“成本为529326美元和23美分”

    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();
    }

如何在没有for循环的情况下在Javascript中使用Django模板标签 - javascript

我想在JavaScript中使用模板变量:我的问题是在javascript代码中使用for循环,for循环之间的所有事情都会重复..但我不想要....下面粘贴了我的代码..有人可以告诉我更好的方法吗这..因为这看起来很丑..这是我的代码: {% block extra_javascript %} <script src="/static/js…

Mongo抛出“元素名称'名称'无效”异常 - c#

我正在更新一个简单的字段。var filterDocument = new BsonDocument { { "name", "alice" } }; var newDocument = new BsonDocument { { "name", "Alice" } }; coll…

T-SQL等价的正则表达式'\ b' - c#

我正在将利用regex的CLR函数转换为SQL函数。我知道SQL Server并不完全支持正则表达式,但是我只需要一种情况就可以搜索单词。搜索字段值:{"Id":1234, "Title": "The quick brown"}.NET中的正则表达式模式:'\b' + '…

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

您如何在列表内部调用一个字符串位置? - python

我一直在做迷宫游戏。我首先决定制作一个迷你教程。游戏开发才刚刚开始,现在我正在尝试使其向上发展。我正在尝试更改PlayerAre变量,但是它不起作用。我试过放在列表内和列表外。maze = ["o","*","*","*","*","*",…