如何将文字金额转换为数字

发布于 2025-01-04 11:31:40 字数 83 浏览 1 评论 0原文

我正在寻找 ac# 函数,它将单词中的金额转换为相对数字。

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

感谢任何人的早期帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北城挽邺 2025-01-11 11:31:41

检查下面的链接,它是用 Python 编写的,但您可以查看逻辑是否有用。

问候

有没有办法将数字单词转换为整数?

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?

想念有你 2025-01-11 11:31:41

Marc Burns 的 ruby gem 可以做到这一点。我最近分叉了它以增加多年来的支持。您可以从 C# 调用 ruby 代码

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

罪歌 2025-01-11 11:31:40

已经测试它可以处理包含多个数字的字符串。

用法: findNumbers("成本为 529,326 美元 23 美分");

输出:"成本为 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();
    }

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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文