是否有一种简单的方法将序数字符串转换为其匹配的数值?

发布于 2024-09-25 23:41:06 字数 1971 浏览 2 评论 0原文

有谁知道有一种方法可以将“第一”、“第十”和“百分之一”等单词转换为相应的数字吗?

样品: “第一”-> 1、 “第二”-> 2、 “第十”-> 10、 “第一百”-> 100

任何算法都可以,但我是用 C# 编写的。

编辑

它并不漂亮,一次只能使用一个单词,但它适合我的目的。也许有人可以改进它,但我没时间了。

 public static int GetNumberFromOrdinalString(string inputString)
    {
        string[] ordinalNumberWords = { "", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth", "sixteenth", "seventeenth", "eighteenth", "nineteenth", "twentieth" };
        string[] ordinalNumberWordsTens = { "", "tenth", "twentieth", "thirtieth", "fortieth", "fiftieth", "sixtieth", "seventieth", "eightieth", "ninetieth" };
        string[] ordinalNumberWordsExtended = {"hundredth", "thousandth", "millionth", "billionth" };

        if (inputString.IsNullOrEmpty() || inputString.Length < 5 || inputString.Contains(" ")) return 0;

        if (ordinalNumberWords.Contains(inputString) || ordinalNumberWordsTens.Contains(inputString))
        {
            var outputMultiplier = ordinalNumberWords.Contains(inputString) ? 1 : 10;
            var arrayToCheck = ordinalNumberWords.Contains(inputString) ? ordinalNumberWords : ordinalNumberWordsTens;

            // Use the loop counter to get our output integer.
            for (int x = 0; x < arrayToCheck.Count(); x++)
            {
                if (arrayToCheck[x] == inputString)
                {
                    return x * outputMultiplier;
                }
            }
        }

        // Check if the number is one of our extended numbers and return the appropriate value.
        if (ordinalNumberWordsExtended.Contains(inputString))
        {
            return inputString == ordinalNumberWordsExtended[0] ? 100 : inputString == ordinalNumberWordsExtended[1] ? 1000 : inputString == ordinalNumberWordsExtended[2] ? 1000000 : 1000000000;
        }

        return 0;
    }

Does anyone know of a method to convert words like "first", "tenth" and "one hundredth" to their numeric equivalent?

Samples:
"first" -> 1,
"second" -> 2,
"tenth" -> 10,
"hundredth" -> 100

Any algorithm will suffice but I'm writing this in C#.

EDIT

It ain't pretty and only works with one word at a time but it suits my purposes. Maybe someone can improve it but I'm out of time.

 public static int GetNumberFromOrdinalString(string inputString)
    {
        string[] ordinalNumberWords = { "", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth", "sixteenth", "seventeenth", "eighteenth", "nineteenth", "twentieth" };
        string[] ordinalNumberWordsTens = { "", "tenth", "twentieth", "thirtieth", "fortieth", "fiftieth", "sixtieth", "seventieth", "eightieth", "ninetieth" };
        string[] ordinalNumberWordsExtended = {"hundredth", "thousandth", "millionth", "billionth" };

        if (inputString.IsNullOrEmpty() || inputString.Length < 5 || inputString.Contains(" ")) return 0;

        if (ordinalNumberWords.Contains(inputString) || ordinalNumberWordsTens.Contains(inputString))
        {
            var outputMultiplier = ordinalNumberWords.Contains(inputString) ? 1 : 10;
            var arrayToCheck = ordinalNumberWords.Contains(inputString) ? ordinalNumberWords : ordinalNumberWordsTens;

            // Use the loop counter to get our output integer.
            for (int x = 0; x < arrayToCheck.Count(); x++)
            {
                if (arrayToCheck[x] == inputString)
                {
                    return x * outputMultiplier;
                }
            }
        }

        // Check if the number is one of our extended numbers and return the appropriate value.
        if (ordinalNumberWordsExtended.Contains(inputString))
        {
            return inputString == ordinalNumberWordsExtended[0] ? 100 : inputString == ordinalNumberWordsExtended[1] ? 1000 : inputString == ordinalNumberWordsExtended[2] ? 1000000 : 1000000000;
        }

        return 0;
    }

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

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

发布评论

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

评论(2

〗斷ホ乔殘χμё〖 2024-10-02 23:41:06

除了我知道“和”这个词应该是从整数到小数的过渡之外,我从未考虑过这么多。就像

一百九十九美元十美分,

而不是

一百九十九美元。

无论如何,任何潜在的解决方案都必须解析输入字符串,引发任何异常或以其他方式返回值。

但首先你必须了解“规则” 这似乎非常武断并且基于传统,但这位绅士似乎是一个很好的起点:

询问数学博士

I've never given this much thought beyond I know the word "and" is supposed to be the transition from whole numbers to decimals. Like

One Hundred Ninety-Nine Dollars and Ten Cents

not

One Hundred and Ninety-Nine Dollars.

Anyways any potential solution would have to parse the input string, raise any exceptions or otherwise return the value.

But first you'd have to know "the rules" This seems to be very arbitrary and based on tradition but this gentleman seems as good a place as any to start:

Ask Dr. Math

云巢 2024-10-02 23:41:06

我认为您最终可能必须将字符串映射到您期望的最大范围内的值,然后按顺序解析字符串并按原样放置值。由于跨数量级和数量级内的常规命名约定非常少,因此我认为没有一种优雅或简单的方法来解析单词以获取其数值。幸运的是,根据格式,您可能只需要映射每个数量级。例如,如果您只期望数字 0-100 并且输入为“九十九”,那么您只需要映射 0-9,然后以 10 为步长映射 10-100。

I think you might end up having to map strings to values up to the maximum range you expect and then parse the string in order and place values as such. Since there's very little regular naming convention across and within order of magnitude, I don't think there's an elegant or easy way to parse a word to get its numeric value. Luckily, depending on the format, you probably only have to map every order of magnitude. For example, if you only expect numbers 0-100 and they are inputted as "ninety-nine" then you only need to map 0-9, then 10-100 in steps of 10.

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