首页 新闻 搜索 专区 学院

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

0
悬赏园豆:30 [已解决问题] 解决于 2018-06-01 06:19

要用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

 

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

新西兰程序员的主页 新西兰程序员 | 初学一级 | 园豆:3
提问于:2015-05-18 10:46
< >
分享
最佳答案
0

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;
            }
        }

收获园豆:20
浅草青晨 | 小虾三级 |园豆:598 | 2015-05-22 11:41
其他回答(2)
0

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

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

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

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

收获园豆:10
幻天芒 | 园豆:36652 (高人七级) | 2015-05-18 16:11

大侠,我写了个处理整数(不包括小数)的函数如下:

 1 private string TranslateDollars(string dollars)  
 2         {
 3 
 4             string rtnStr = "";
 5             string newDar = dollars;
 6             bool isTranslated = false;
 7             bool isZeroBegin = false;
 8             double darNum = Convert.ToDouble(dollars); 
 9             if (darNum > 0)
10             {
11                 isZeroBegin = dollars.StartsWith("0");
12                 //if (isZeroBegin)
13                 //    newDar = TrimZeroStart(dollars);
14                 int darLength = dollars.Length;
15                 int numPos = 0; 
16                 string darName = ""; 
17                 switch (darLength)
18                 {
19                     case 1:
20                         rtnStr = ConvertOneNumber(newDar);
21                         isTranslated = true;
22                         break;
23                     case 2:
24                         rtnStr = ConvertTenNumber(newDar);
25                         isTranslated = true;
26                         break;
27                     case 3:
28                         numPos = (darLength % 3) + 1;
29                       
30                         darName = " Hundred ";
31                         break;
32                     case 4:
33                     case 5:
34                     case 6:
35                         numPos = (darLength % 4) + 1;
36                       
37                         darName = " Thousand ";
38                         break;
39                     case 7:
40                     case 8:
41                     case 9:
42                         numPos = (darLength % 7) + 1;
43                       
44                         darName = " Million ";
45                         break;
46                     case 10:
47                         numPos = (darLength % 10) + 1;
48                       
49                         darName = " Billion ";
50                         break;
51                     default:
52                         isTranslated = true;
53                         break;
54 
55                 }
56                 if (!isTranslated)
57                 {
58                     rtnStr = TranslateDollars(newDar.Substring(0, numPos)) + darName + TranslateDollars(newDar.Substring(numPos));
59                     //if (isZeroBegin) rtnStr = " and " + rtnStr.Trim();
60                 }
61 
62                 if (rtnStr.Trim().Equals(darName.Trim())) rtnStr = "";
63  
64             }
65 
66             return rtnStr.Trim();
67 
68 
69         }

但是这个函数目前有点问题,比如

1000050  会转换为 One Million Thousand Hundred Fifty Dollars

应该是  One Million and Fifty Dollars

1000010000.15 会转换为

One Billion Million Hundred Ten Thousand Dollars and Fifteen Cents

有没有哪位大侠能帮我改改这个代码 ?

我实在是搞不定

谢谢了

支持(0) 反对(0) 新西兰程序员 | 园豆:3 (初学一级) | 2015-05-20 07:12

@新西兰程序员: 

给你一个参考,金额转人民币大写的,性质和这个差不多:https://github.com/hstarorg/Hstar.Utility/blob/master/Hstar.Utility/Helpers/RmbHelper.cs

支持(0) 反对(0) 幻天芒 | 园豆:36652 (高人七级) | 2015-05-20 09:40
0

面试写着 有点变态

_York | 园豆:14 (初学一级) | 2018-06-01 10:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册