使用 C#.Net 将整数转换为英文单词

发布于 2024-09-17 01:06:01 字数 3554 浏览 6 评论 0原文

任何人都可以帮我调试这个...执行以下代码时出现以下错误..

Error   1   Cannot implicitly convert type 'string' to 'long'
Error   2   The name 'inputNum' does not exist in the current contex

    protected void Button1_Click(object sender, EventArgs e) {
        var englishTranslation = IntegerToWords(inputNum);
    }

    public string IntegerToWords(long inputNum) {
        int dig1, dig2, dig3, level = 0, lasttwo, threeDigits;

        var retval = "";
        var x = "";
        string[] ones = {
                            "zero",
                            "one",
                            "two",
                            "three",
                            "four",
                            "five",
                            "six",
                            "seven",
                            "eight",
                            "nine",
                            "ten",
                            "eleven",
                            "twelve",
                            "thirteen",
                            "fourteen",
                            "fifteen",
                            "sixteen",
                            "seventeen",
                            "eighteen",
                            "nineteen"
                        };
        string[] tens =
            {
                "zero",
                "ten",
                "twenty",
                "thirty",
                "forty",
                "fifty",
                "sixty",
                "seventy",
                "eighty",
                "ninety"
            };
        string[] thou =
            {
                "",
                "thousand",
                "million",
                "billion",
                "trillion",
                "quadrillion",
                "quintillion"
            };

        var isNegative = false;
        if (inputNum < 0) {
            isNegative = true;
            inputNum *= -1;
        }

        if (inputNum == 0)
            return ("zero");

        var s = inputNum.ToString();

        while (s.Length > 0) {
            // Get the three rightmost characters
            x = (s.Length < 3) ? s : s.Substring(s.Length - 3, 3);

            // Separate the three digits
            threeDigits = int.Parse(x);
            lasttwo = threeDigits%100;
            dig1 = threeDigits/100;
            dig2 = lasttwo/10;
            dig3 = (threeDigits%10);

            // append a "thousand" where appropriate
            if (level > 0 && dig1 + dig2 + dig3 > 0) {
                retval = thou[level] + " " + retval;
                retval = retval.Trim();
            }

            // check that the last two digits is not a zero
            if (lasttwo > 0) {
                if (lasttwo < 20) // if less than 20, use "ones" only
                    retval = ones[lasttwo] + " " + retval;
                else // otherwise, use both "tens" and "ones" array
                    retval = tens[dig2] + " " + ones[dig3] + " " + retval;
            }

            // if a hundreds part is there, translate it
            if (dig1 > 0)
                retval = ones[dig1] + " hundred " + retval;

            s = (s.Length - 3) > 0 ? s.Substring(0, s.Length - 3) : "";
            level++;
        }

        while (retval.IndexOf("  ") > 0)
            retval = retval.Replace("  ", " ");

        retval = retval.Trim();

        if (isNegative)
            retval = "negative " + retval;

        return (retval);
    }

我是编程新手...

can anybody help me to debug this... i have the following errors when executing the bolow code..

Error   1   Cannot implicitly convert type 'string' to 'long'
Error   2   The name 'inputNum' does not exist in the current contex

    protected void Button1_Click(object sender, EventArgs e) {
        var englishTranslation = IntegerToWords(inputNum);
    }

    public string IntegerToWords(long inputNum) {
        int dig1, dig2, dig3, level = 0, lasttwo, threeDigits;

        var retval = "";
        var x = "";
        string[] ones = {
                            "zero",
                            "one",
                            "two",
                            "three",
                            "four",
                            "five",
                            "six",
                            "seven",
                            "eight",
                            "nine",
                            "ten",
                            "eleven",
                            "twelve",
                            "thirteen",
                            "fourteen",
                            "fifteen",
                            "sixteen",
                            "seventeen",
                            "eighteen",
                            "nineteen"
                        };
        string[] tens =
            {
                "zero",
                "ten",
                "twenty",
                "thirty",
                "forty",
                "fifty",
                "sixty",
                "seventy",
                "eighty",
                "ninety"
            };
        string[] thou =
            {
                "",
                "thousand",
                "million",
                "billion",
                "trillion",
                "quadrillion",
                "quintillion"
            };

        var isNegative = false;
        if (inputNum < 0) {
            isNegative = true;
            inputNum *= -1;
        }

        if (inputNum == 0)
            return ("zero");

        var s = inputNum.ToString();

        while (s.Length > 0) {
            // Get the three rightmost characters
            x = (s.Length < 3) ? s : s.Substring(s.Length - 3, 3);

            // Separate the three digits
            threeDigits = int.Parse(x);
            lasttwo = threeDigits%100;
            dig1 = threeDigits/100;
            dig2 = lasttwo/10;
            dig3 = (threeDigits%10);

            // append a "thousand" where appropriate
            if (level > 0 && dig1 + dig2 + dig3 > 0) {
                retval = thou[level] + " " + retval;
                retval = retval.Trim();
            }

            // check that the last two digits is not a zero
            if (lasttwo > 0) {
                if (lasttwo < 20) // if less than 20, use "ones" only
                    retval = ones[lasttwo] + " " + retval;
                else // otherwise, use both "tens" and "ones" array
                    retval = tens[dig2] + " " + ones[dig3] + " " + retval;
            }

            // if a hundreds part is there, translate it
            if (dig1 > 0)
                retval = ones[dig1] + " hundred " + retval;

            s = (s.Length - 3) > 0 ? s.Substring(0, s.Length - 3) : "";
            level++;
        }

        while (retval.IndexOf("  ") > 0)
            retval = retval.Replace("  ", " ");

        retval = retval.Trim();

        if (isNegative)
            retval = "negative " + retval;

        return (retval);
    }

I am new to programming...

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

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

发布评论

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

评论(1

烂柯人 2024-09-24 01:06:01

这些错误不是逻辑错误。
1. 检查发生错误的行号。使用long.Parse(str)将字符串转换为long。
2. Button1_Click 函数中未声明变量 inputNum。声明它,为其分配必须转换的值(可能来自文本框?)。

The Errors are not logical errors.
1. Check the line-number where the error has occurred. Use the long.Parse(str) to convert the string to long.
2. The variable inputNum is not declared in the Button1_Click function. Declare it, assign it the value (from a textbox maybe?) which has to be converted.

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