Tech Neo 创造一个纯粹高质的技术交流平台

紧急求助C#面试题,各位大虾帮帮我

要用c#写一个web application.包括unit test, 把数字转换为英语单词的货币表示

比如:

130423.05 =〉One Hundred and Thirty Thousand Four Hundred and Twenty Three Dollars and Five Gents

1000010000.15 => One Billion Ten Thousand Dollars and Fifteen cents

1.151 => One Dollar and Fifteen cents

 

非常紧急,还望园子内有经验的大虾帮帮我,非常感谢了

请先 登录 后评论

最佳答案 2018-06-08 15:04

public class Number2English
        {
            private readonly string[] StrNO = new string[19];//1-19的英文
            private readonly string[] StrTens = new string[9];//整十英文
            private readonly string[] Unit = new string[9];//英文单位

            public string TranslateDollars(double dollars)
            {
                string Str, BeforePoint, AfterPoint, temp;
                int Bit;   //单位的个数
                string CurString;//当前字符串
                int NumLen;
                Init();
                Str = (Math.Round(dollars, 2).ToString("#0.00"));//取两位有效数字
                if (Str.IndexOf(".") == -1)
                {
                    BeforePoint = Str;
                    AfterPoint = "";
                }
                else
                {
                    BeforePoint = Str.Substring(0, Str.IndexOf("."));
                    AfterPoint = Str.Substring(Str.IndexOf(".") + 1, Str.Length - Str.IndexOf(".") - 1);
                }
                //处理整数部分
                if (BeforePoint.Length > 12)
                {
                    return null;
                }
                Str = "";
                if (dollars >= 1)//整数部分不为0
                {
                    while (BeforePoint.Length > 0)
                    {
                        NumLen = Len(BeforePoint);
                        if (NumLen % 3 == 0)
                        {
                            CurString = Left(BeforePoint, 3);
                            BeforePoint = Right(BeforePoint, NumLen - 3);
                        }
                        else
                        {
                            CurString = Left(BeforePoint, (NumLen % 3));
                            BeforePoint = Right(BeforePoint, NumLen - (NumLen % 3));
                        }
                        Bit = Len(BeforePoint) / 3;
                        temp = DecodeHundred(CurString);
                        if (Bit == 0 && Len(CurString) == 3)
                        {
                            if (Convert.ToInt32(Left(CurString, 1)) != 0 & Convert.ToInt32(Right(CurString, 2)) != 0)
                            {
                                temp = Left(temp, temp.IndexOf(Unit[3]) + Len(Unit[3])) + Unit[7] + " " +
                                            Right(temp, Len(temp) - (temp.IndexOf(Unit[3]) + Len(Unit[3])));
                            }
                            else
                            {
                                temp = Unit[7] + " " + temp;
                            }
                        }
                        if (Bit == 0)
                        {
                            Str = Convert.ToString(Str + " " + temp).Trim();
                        }
                        else
                        {
                            if (temp != "")
                            {
                                Str = Convert.ToString(Str + " " + temp + " " + Unit[Bit - 1]).Trim();
                            }
                        }
                        if (Left(Str, 3) == Unit[7])
                        {
                            Str = Convert.ToString(Right(Str, Len(Str) - 3)).Trim();
                        }
                        if (BeforePoint == Len(BeforePoint).ToString())
                        {
                            return "";
                        }
                    }
                    BeforePoint = Str+" ";
                }
                else//整数部分为0
                {
                    BeforePoint = "";
                }
               // 处理小数部分
                if ((dollars *100)%100 > 0)//如果存在小数
                {
                    if (DecodeHundred(AfterPoint) =="One")
                    {
                        if (Convert.ToUInt64(dollars) == 1)
                        {
                            AfterPoint = Unit[8] + " And " + DecodeHundred(AfterPoint) + " " + Unit[6];//单数
                        }
                        else if (Convert.ToUInt64(dollars) == 0)
                        {
                            AfterPoint = DecodeHundred(AfterPoint) + " And " + Unit[6];//单数
                        }
                        else
                        {
                            AfterPoint = Unit[5] + " And " + DecodeHundred(AfterPoint) + " " + Unit[6];//单数
                        }
                    }
                    else
                    {
                        if (Convert.ToUInt64(dollars) == 1)
                        {
                            AfterPoint = Unit[8] + " And " + DecodeHundred(AfterPoint) + " " + Unit[6] + "s";//复数
                        }
                        else if (Convert.ToUInt64(dollars) == 0)
                        {
                            AfterPoint = DecodeHundred(AfterPoint) + " " + Unit[6] + "s";//复数
                        }
                        else
                        {
                            AfterPoint = Unit[5] + " And " + DecodeHundred(AfterPoint) + " " + Unit[6] + "s";//复数
                        }
                    }
                }
                else//如果不存在小数
                {
                    if (Convert.ToUInt64(dollars) == 1)
                    {
                        AfterPoint = Unit[8];
                    }
                    else
                    {
                        AfterPoint = Unit[5];
                    }
                   
                }
                return (BeforePoint + AfterPoint);
            }

            private void Init()
            {
                if (StrNO[0] != "One")
                {
                    StrNO[0] = "One";
                    StrNO[1] = "Two";
                    StrNO[2] = "Three";
                    StrNO[3] = "Four";
                    StrNO[4] = "Five";
                    StrNO[5] = "Six";
                    StrNO[6] = "Seven";
                    StrNO[7] = "Eight";
                    StrNO[8] = "Nine";
                    StrNO[9] = "Ten";
                    StrNO[10] = "Eleven";
                    StrNO[11] = "Twelve";
                    StrNO[12] = "Thirteen";
                    StrNO[13] = "Fourteen";
                    StrNO[14] = "Fifteen";
                    StrNO[15] = "Sixteen";
                    StrNO[16] = "Seventeen";
                    StrNO[17] = "Eighteen";
                    StrNO[18] = "Nineteen";
                    StrTens[0] = "Ten";
                    StrTens[1] = "Twenty";
                    StrTens[2] = "Thirty";
                    StrTens[3] = "Forty";
                    StrTens[4] = "Fifty";
                    StrTens[5] = "Sixty";
                    StrTens[6] = "Seventy";
                    StrTens[7] = "Eighty";
                    StrTens[8] = "Ninety";
                    Unit[0] = "Thousand";
                    Unit[1] = "Million";
                    Unit[2] = "Billion";
                    Unit[3] = "Hundred";
                    Unit[4] = "Only";
                    Unit[5] = "Dollars";
                    Unit[6] = "Cent";
                    Unit[7] = "";
                    Unit[8] = "Dollar";
                }
            }

            private string DecodeHundred(string HundredString)
            {
                int temp;
                string rtn = "";
                if (Len(HundredString) > 0 && Len(HundredString) <= 3)
                {
                    switch (Len(HundredString))
                    {
                        case 1:
                            temp = Convert.ToInt32(HundredString);
                            if (temp != 0)
                            {
                                rtn = StrNO[temp - 1];
                            }
                            break;
                        case 2:
                            temp = Convert.ToInt32(HundredString);
                            if (temp != 0)
                            {
                                if ((temp < 20))
                                {
                                    rtn = StrNO[temp - 1];
                                }
                                else
                                {
                                    if (Convert.ToUInt64(Right(HundredString, 1)) == 0)
                                    {
                                        rtn = StrTens[Convert.ToInt32(temp / 10) - 1];
                                    }
                                    else
                                    {
                                        rtn = Convert.ToString(StrTens[Convert.ToInt32(temp / 10) - 1] + "-" +
                                                                 StrNO[Convert.ToInt32(Right(HundredString, 1)) - 1]);
                                    }
                                }
                            }
                            break;
                        case 3:
                            if (Convert.ToUInt64(Left(HundredString, 1)) != 0)//百位数字不为空
                            {
                                rtn = Convert.ToString(StrNO[Convert.ToInt32(Left(HundredString, 1)) - 1] + " " + Unit[3] +
                                                             " And " + DecodeHundred(Right(HundredString, 2)));
                            }
                            else
                            {
                                if (Convert.ToUInt64(Right(HundredString, 2)) != 0)
                                {
                                    rtn = "And "+DecodeHundred(Right(HundredString, 2));
                                }
                                else
                                {
                                    rtn = DecodeHundred(Right(HundredString, 2));
                                }
                            }
                            break;
                        default:
                            break;
                    }
                }
                return rtn;
            }

            private string Left(string str, int n)
            {
                return str.Substring(0, n);
            }

            private string Right(string str, int n)
            {
                return str.Substring(str.Length - n, n);
            }

            private int Len(string str)
            {
                return str.Length;
            }
        }

请先 登录 后评论

其它 2 个回答

博问用户

根据.分割,然后在各自算数字转英文,记住几个关键点就行了。

1、3位是分界点,千,百万,十亿。

2、3位中,如果百位是0,那么需要加and

3、看最后一个示例,是保留两位小数

请先 登录 后评论
博问用户

面试写着 有点变态

请先 登录 后评论