如何在c#中将整数转换为单词?

如何在c#中将整数转换为单词?,c#,string,winforms,integer,C#,String,Winforms,Integer,我在文本编辑中得到数字(4552),我需要将其转换为单词“四千五百五十二” 如何完成这项任务?我不喜欢有人问问题而不去搜索谷歌,因为这里有一段来自谷歌的代码 使用系统; 使用System.Collections.Generic; 使用系统文本; 名称空间NumWords { 班级计划 { //该程序处理负双精度和正双精度 静态串NumWordsWrapper(双n) { 字串=”; 双内点; 双depart=0; 如果(n==0) 返回“零”; 试一试{ string[]splitter=n.T

我在文本编辑中得到数字(4552),我需要将其转换为单词“四千五百五十二”


如何完成这项任务?

我不喜欢有人问问题而不去搜索谷歌,因为这里有一段来自谷歌的代码

使用系统;
使用System.Collections.Generic;
使用系统文本;
名称空间NumWords
{
班级计划
{
//该程序处理负双精度和正双精度
静态串NumWordsWrapper(双n)
{
字串=”;
双内点;
双depart=0;
如果(n==0)
返回“零”;
试一试{
string[]splitter=n.ToString().Split('.');
intPart=double.Parse(拆分器[0]);
decPart=double.Parse(拆分器[1]);
}抓住{
intPart=n;
}
单词=NumWords(intPart);
如果(depart>0){
如果(单词!=“”)
文字+=“和”;
int counter=decPart.ToString().Length;
开关(计数器){
案例1:单词+=NumWords(depart)+“十分之一”;中断;
第二种情况:单词+=NumWords(depart)+“百分之一”;中断;
第三种情况:单词+=NumWords(depart)+“千分之一”换行符;
第四种情况:单词+=NumWords(depart)+“万分之一”中断;
第五种情况:单词+=NumWords(depart)+“十万分之一”中断;
案例6:单词+=NumWords(depart)+“百万分之一”中断;
案例7:单词+=NumWords(depart)+“千万分之一”中断;
}
}
返回单词;
}
静态字符串NumWords(double n)//将double转换为单词
{
字符串[]numbersArr=新字符串[]{“一”、“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”、“十”、“十一”、“十二”、“十三”、“十四”、“十五”、“十六”、“十七”、“十八”、“十九”};
字符串[]tensArr=新字符串[]{“二十”、“三十”、“四十”、“五十”、“六十”、“七十”、“八十”、“九十”};
字符串[]后缀sarr=新字符串[]{“千”、“百万”、“十亿”、“万亿”、“千万”、“千万”、“千万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”、“百万”等);
字串=”;
布尔十=假;
if(n<0){
词语+=“否定”;
n*=-1;
}
整数幂=(后缀长度+1)*3;
同时(功率>3){
双功率=数学功率(10,功率);
如果(n>=功率){
如果(n%pow>0){
单词+=NumWords(Math.Floor(n/pow))+“+supplexesarr[(power/3)-1]+”;
}否则如果(n%pow==0){
单词+=NumWords(Math.Floor(n/pow))+“”+supplexesarr[(power/3)-1];
}
n%=功率;
}
功率-=3;
}
如果(n>=1000){
如果(n%1000>0)单词+=NumWords(数学地板(n/1000))+“千”;
其他单词+=NumWords(数学楼层(n/1000))+“千”;
n%=1000;
}
如果(01){
如果(单词!=“”)
单词+=”;
单词+=tensArr[(int)n/10-2];
十=真;
n%=10;
}
如果(n<20&&n>0){
if(单词!=“”&&tens==false)
单词+=”;
单词+=(十?-“+numbersArr[(int)n-1]:numbersArr[(int)n-1]);
n-=数学楼层(n);
}
}
返回单词;
}
静态void Main(字符串[]参数)
{
Console.Write(“输入要转换为单词的数字:”);
Double n=Double.Parse(Console.ReadLine());
Console.WriteLine(“{0}”,NumWordsWrapper(n));
}
}
}

提问时请更加准确。Devexpress与您的任务无关。我希望这会有所帮助:@sri hari,在发布问题之前,您可能希望搜索堆栈溢出。类似的问题有很多。
using System;
using System.Collections.Generic;
using System.Text;

namespace NumWords
{
    class Program
    {
        // PROGRAM HANDLES NEGATIVE AND POSITIVE DOUBLES


        static String NumWordsWrapper(double n)
        {
            string words = "";
            double intPart;
            double decPart = 0;
            if (n == 0)
                return "zero";
            try {
                string[] splitter = n.ToString().Split('.');
                intPart = double.Parse(splitter[0]);
                decPart = double.Parse(splitter[1]);
            } catch {
                intPart = n;
            }

            words = NumWords(intPart);

            if (decPart > 0) {
                if (words != "")
                    words += " and ";
                int counter = decPart.ToString().Length;
                switch (counter) {
                    case 1: words += NumWords(decPart) + " tenths"; break;
                    case 2: words += NumWords(decPart) + " hundredths"; break;
                    case 3: words += NumWords(decPart) + " thousandths"; break;
                    case 4: words += NumWords(decPart) + " ten-thousandths"; break;
                    case 5: words += NumWords(decPart) + " hundred-thousandths"; break;
                    case 6: words += NumWords(decPart) + " millionths"; break;
                    case 7: words += NumWords(decPart) + " ten-millionths"; break;
                }
            }
            return words;
        }

        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;

        }
        static void Main(string[] args)
        {
            Console.Write("Enter a number to convert to words: ");
            Double n = Double.Parse(Console.ReadLine());

            Console.WriteLine("{0}", NumWordsWrapper(n));
        }
    }
}