数字转英文

<PRE class="brush: csharp; auto-links: true; collapse: true; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">/*
 * by:stefanie 
 * date:2009-12-04
 */

namespace ConsoleApplication1
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    class Program
    {
       private static Dictionary&lt;int, string&gt; dicUnit;
       private static Dictionary&lt;int, string&gt; dicPlace;

       static string Splite_Place = " {0} ";
       static string Splite_Hundreds_Tens = " and ";
       static string Splite_Tens_Unit = "-";
       static string Splite_Point = "point";

        //template
       static string Unit_Tens_Place = "{0}";//20,30,40...90
       static string Hundreds_Place = "{0} hundred";

        static void Main(string[] args)
        {
            initDic();
            string result = "";
            Console.WriteLine("Please input a Number:");
            string input = Console.ReadLine();
            int pointIndex =input.IndexOf('.');
            string integer = input;
            string dec ="";
            if(pointIndex != -1 )
            {
                integer = input.Substring(0, pointIndex);
                dec = input.Substring(pointIndex + 1);
            }
          
            int len = integer.Length;
            if (len &gt; 15)
            {
                Console.WriteLine("Input number is too long");
                return;
            }

            int index = (len - 1) / 3;
            int bwNum, swNum, gwNum;
            string str;
            for (int i = 0; i &lt;= index; i++)
            {
                str = integer.Substring(i*3);
                if (str.Length &gt; 3)
                {
                    str = integer.Substring(i * 3, 3);
                }
              
                if (str.Length == 3)
                {
                    bwNum = int.Parse(str.Substring(0, 1));
                    swNum = int.Parse(str.Substring(1, 1));
                    gwNum = int.Parse(str.Substring(2, 1));
                    result += GetStr(bwNum, swNum, gwNum);
                }
                else if (len == 2)
                {
                    swNum = int.Parse(str.Substring(0, 1));
                    gwNum = int.Parse(str.Substring(1, 1));
                    result += GetStr(swNum, gwNum);
                }
                else if (len == 1)
                {
                    gwNum = int.Parse(str.Substring(0, 1));
                    if (gwNum != 0)
                    {
                        result += GetStr(gwNum);
                    }
                }

                if ((index - i) != 0)
                {
                    result += string.Format(Splite_Place, dicPlace[index - i]);
                }
            }

            if (pointIndex != -1)
            {
                if (result != "")
                {
                    result += " ";
                }
                result += Splite_Point;
                
                for (int i = 0; i &lt; dec.Length; i++)
                {
                    result += " " + GetStr(int.Parse(dec.Substring(i, 1)));
                }
            }

            result += " dollar";

            Console.WriteLine(result);
            Console.Read();
        }

        private static string GetStr(int gwNum)
        {
            return string.Format(Unit_Tens_Place, dicUnit[gwNum]);
        }

        private static string GetStr(int swNum, int gwNum)
        {
            string r = "";
            
            if (swNum &gt; 1)
            {
                r += Splite_Hundreds_Tens + string.Format(Unit_Tens_Place, dicUnit[swNum * 10]);
                if (gwNum != 0)
                {
                    r += Splite_Tens_Unit + string.Format(Unit_Tens_Place, dicUnit[gwNum]);
                }
            }
            else if (swNum &gt; 0 &amp;&amp; swNum &lt;= 1)
            {
                r = Splite_Hundreds_Tens + string.Format(Unit_Tens_Place, dicUnit[swNum * 10 + gwNum]);
            }
            else if (swNum == 0 &amp;&amp; gwNum &gt; 0)
            {
                r += Splite_Hundreds_Tens + GetStr(gwNum);
            }
            return r;
        }

        private static string GetStr(int bwNum, int swNum, int gwNum)
        {
            string r = "";

            if (bwNum &gt; 0)
            {
                r = string.Format(Hundreds_Place, dicUnit[bwNum]);
            }
            return r += GetStr(swNum, gwNum);
        }



        private static void initDic()
        {
            dicUnit = new Dictionary&lt;int, string&gt;();
            dicUnit.Add(0, "zero");
            dicUnit.Add(1, "one");
            dicUnit.Add(2, "two");
            dicUnit.Add(3, "three");
            dicUnit.Add(4, "four");
            dicUnit.Add(5, "five");
            dicUnit.Add(6, "six");
            dicUnit.Add(7, "seven");
            dicUnit.Add(8, "eight");
            dicUnit.Add(9, "nine");
            dicUnit.Add(10, "ten");
            dicUnit.Add(11, "eleven");
            dicUnit.Add(12, "twelve");
            dicUnit.Add(13, "thirteen");
            dicUnit.Add(14, "fourteen");
            dicUnit.Add(15, "fifteen
/*
 * by:stefanie 
 * date:2009-12-04
 */

namespace ConsoleApplication1
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    class Program
    {
       private static Dictionary<int, string> dicUnit;
       private static Dictionary<int, string> dicPlace;

       static string Splite_Place = " {0} ";
       static string Splite_Hundreds_Tens = " and ";
       static string Splite_Tens_Unit = "-";
       static string Splite_Point = "point";

        //template
       static string Unit_Tens_Place = "{0}";//20,30,40...90
       static string Hundreds_Place = "{0} hundred";

        static void Main(string[] args)
        {
            initDic();
            string result = "";
            Console.WriteLine("Please input a Number:");
            string input = Console.ReadLine();
            int pointIndex =input.IndexOf('.');
            string integer = input;
            string dec ="";
            if(pointIndex != -1 )
            {
                integer = input.Substring(0, pointIndex);
                dec = input.Substring(pointIndex + 1);
            }
          
            int len = integer.Length;
            if (len > 15)
            {
                Console.WriteLine("Input number is too long");
                return;
            }

            int index = (len - 1) / 3;
            int bwNum, swNum, gwNum;
            string str;
            for (int i = 0; i <= index; i++)
            {
                str = integer.Substring(i*3);
                if (str.Length > 3)
                {
                    str = integer.Substring(i * 3, 3);
                }
              
                if (str.Length == 3)
                {
                    bwNum = int.Parse(str.Substring(0, 1));
                    swNum = int.Parse(str.Substring(1, 1));
                    gwNum = int.Parse(str.Substring(2, 1));
                    result += GetStr(bwNum, swNum, gwNum);
                }
                else if (len == 2)
                {
                    swNum = int.Parse(str.Substring(0, 1));
                    gwNum = int.Parse(str.Substring(1, 1));
                    result += GetStr(swNum, gwNum);
                }
                else if (len == 1)
                {
                    gwNum = int.Parse(str.Substring(0, 1));
                    if (gwNum != 0)
                    {
                        result += GetStr(gwNum);
                    }
                }

                if ((index - i) != 0)
                {
                    result += string.Format(Splite_Place, dicPlace[index - i]);
                }
            }

            if (pointIndex != -1)
            {
                if (result != "")
                {
                    result += " ";
                }
                result += Splite_Point;
                
                for (int i = 0; i < dec.Length; i++)
                {
                    result += " " + GetStr(int.Parse(dec.Substring(i, 1)));
                }
            }

            result += " dollar";

            Console.WriteLine(result);
            Console.Read();
        }

        private static string GetStr(int gwNum)
        {
            return string.Format(Unit_Tens_Place, dicUnit[gwNum]);
        }

        private static string GetStr(int swNum, int gwNum)
        {
            string r = "";
            
            if (swNum > 1)
            {
                r += Splite_Hundreds_Tens + string.Format(Unit_Tens_Place, dicUnit[swNum * 10]);
                if (gwNum != 0)
                {
                    r += Splite_Tens_Unit + string.Format(Unit_Tens_Place, dicUnit[gwNum]);
                }
            }
            else if (swNum > 0 && swNum <= 1)
            {
                r = Splite_Hundreds_Tens + string.Format(Unit_Tens_Place, dicUnit[swNum * 10 + gwNum]);
            }
            else if (swNum == 0 && gwNum > 0)
            {
                r += Splite_Hundreds_Tens + GetStr(gwNum);
            }
            return r;
        }

        private static string GetStr(int bwNum, int swNum, int gwNum)
        {
            string r = "";

            if (bwNum > 0)
            {
                r = string.Format(Hundreds_Place, dicUnit[bwNum]);
            }
            return r += GetStr(swNum, gwNum);
        }



        private static void initDic()
        {
            dicUnit = new Dictionary<int, string>();
            dicUnit.Add(0, "zero");
            dicUnit.Add(1, "one");
            dicUnit.Add(2, "two");
            dicUnit.Add(3, "three");
            dicUnit.Add(4, "four");
            dicUnit.Add(5, "five");
            dicUnit.Add(6, "six");
            dicUnit.Add(7, "seven");
            dicUnit.Add(8, "eight");
            dicUnit.Add(9, "nine");
            dicUnit.Add(10, "ten");
            dicUnit.Add(11, "eleven");
            dicUnit.Add(12, "twelve");
            dicUnit.Add(13, "thirteen");
            dicUnit.Add(14, "fourteen");
            dicUnit.Add(15, "fifteen");
            dicUnit.Add(16, "sixteen");
            dicUnit.Add(17, "seventeen");
            dicUnit.Add(18, "eighteen");
            dicUnit.Add(19, "nineteen");
            dicUnit.Add(20, "twenty");
            dicUnit.Add(30, "thirty");
            dicUnit.Add(40, "forty");
            dicUnit.Add(50, "fifteen");
            dicUnit.Add(60, "sixteen");
            dicUnit.Add(70, "seventeen");
            dicUnit.Add(80, "eighteen");
            dicUnit.Add(90, "nineteen");


            dicPlace = new Dictionary<int,string>();
            dicPlace.Add(0, "");
            dicPlace.Add(1, "thousand");
            dicPlace.Add(2, "million");
            dicPlace.Add(3, "billion");
            dicPlace.Add(4, "trillion");
        }
    }
}
");
            dicUnit.Add(16, "sixteen");
            dicUnit.Add(17, "seventeen");
            dicUnit.Add(18, "eighteen");
            dicUnit.Add(19, "nineteen");
            dicUnit.Add(20, "twenty");
            dicUnit.Add(30, "thirty");
            dicUnit.Add(40, "forty");
            dicUnit.Add(50, "fifteen");
            dicUnit.Add(60, "sixteen");
            dicUnit.Add(70, "seventeen");
            dicUnit.Add(80, "eighteen");
            dicUnit.Add(90, "nineteen");


            dicPlace = new Dictionary&lt;int,string&gt;();
            dicPlace.Add(0, "");
            dicPlace.Add(1, "thousand");
            dicPlace.Add(2, "million");
            dicPlace.Add(3, "billion");
            dicPlace.Add(4, "trillion");
        }
    }
}</PRE>
posted @ 2009-12-04 16:37  stefanie  阅读(569)  评论(0编辑  收藏  举报