如何使用控制台应用程序中的代码到表单应用程序

时间:2016-07-05 08:33:56

标签: c# winforms visual-studio console-application

嘿,我希望能够在表单中使用此代码,但我没有足够的经验,并想知道如何以及在何处更改此代码以便能够在FORM中使用它尝试更改为public void etc但是只收到错误消息

    static String NumWords(double n) //converts double to words
    {
        string[] numbersArr = new string[] { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
        string[] tensArr = new string[] { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninty" };
        string[] suffixesArr = new string[] { "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "Quattuordecillion", "Quindecillion", "Sexdecillion", "Septdecillion", "Octodecillion", "Novemdecillion", "Vigintillion" };
        string words = "";

        bool tens = false;

        if (n < 0)
        {
            words += "negative ";
            n *= -1;
        }

        int power = (suffixesArr.Length + 1) * 3;

        while (power > 3)
        {
            double pow = Math.Pow(10, power);
            if (n >= pow)
            {
                if (n % pow > 0)
                {
                    words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1] + ", ";
                }
                else if (n % pow == 0)
                {
                    words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1];
                }
                n %= pow;
            }
            power -= 3;
        }
        if (n >= 1000)
        {
            if (n % 1000 > 0) words += NumWords(Math.Floor(n / 1000)) + " thousand, ";
            else words += NumWords(Math.Floor(n / 1000)) + " thousand";
            n %= 1000;
        }
        if (0 <= n && n <= 999)
        {
            if ((int)n / 100 > 0)
            {
                words += NumWords(Math.Floor(n / 100)) + " hundred";
                n %= 100;
            }
            if ((int)n / 10 > 1)
            {
                if (words != "")
                    words += " ";
                words += tensArr[(int)n / 10 - 2];
                tens = true;
                n %= 10;
            }

            if (n < 20 && n > 0)
            {
                if (words != "" && tens == false)
                    words += " ";
                words += (tens ? "-" + numbersArr[(int)n - 1] : numbersArr[(int)n - 1]);
                n -= Math.Floor(n);
            }
        }

        return words;

    }

1 个答案:

答案 0 :(得分:0)

您可以在按钮点击envent上包含此功能,也可以在文本框的更改事件中调用此函数,该事件将用于输入数字值。

  1. 如果您从按钮单击调用此功能,则只需将文本框值作为此功能的输入
  2. // COnvertBtn is button id, on click of which function will get call //But make sure that text box named as txtNumber should contain number void ConvertBtn_Click(Object sender, EventArgs e) { string number = NumWords(Convert.ToDouble(txtNumber.Text)); }

    1. 否则你可以直接从javascript调用函数,这里你需要对函数进行一些修改,因为输入的数据类型将是文本

    2. 此致 满足