前几天,听到群的人说的一道面试题,将double类型变成钱类型
也就把1变成大写的1,2变成大写的2等等
当写的一遍后才知道问题很多
要点:
1.像102.34应该是一百零二点三四(当然这是大写的)
2.像1004.4应该是一千零四点四I(也是大写)
3.像334345应该是三十三万四千三百四十五.(也是大写)
4像330这种是三百三十(也是大写)
以下是粗略的代码,只写的了亿级,要更高更改.
class Program
{
static void Main(string[] args)
{
bool flag = false;
double a = 23493498.409;
int len = ((int)a).ToString().Length;
string str = a.ToString();
Console.WriteLine(" {0},{1}", str, len);
char[] carr = str.ToCharArray();
if (len >= 5 && carr[len - 5] != '0')
flag = true;
for (int i = 0; i < carr.Length; i++, len--)
{
if (carr[i] == '0' && carr[i - 1] == '0' ||(carr[i]=='0'&& carr[i+1]=='.'))
;
else
Console.Write("{0}", new cast_no().fun(carr[i]));
if (carr[i] != '0' && len > 0)
{
Console.Write("{0}", new cast_no().fun1(len, flag));
}
}
Console.Read();
}
}
public class cast_no
{
public string fun(char i)
{
string str = string.Empty;
switch (i)
{
case '1':
str = "一";
break;
case '2':
str = "二";
break;
case '3':
str = "三";
break;
case '4':
str = "四";
break;
case '5':
str = "五";
break;
case '6':
str = "六";
break;
case '7':
str = "七";
break;
case '8':
str = "八";
break;
case '9':
str = "九";
break;
case '0':
str = "零";
break;
case '.':
str = "点";
break;
}
return str;
}
public string fun1(int i, bool flag)
{
string str = string.Empty;
switch (i)
{
case 1:
break;
case 2:
str = "十";
break;
case 3:
str = "百";
break;
case 4:
str = "千";
break;
case 5:
str = "万";
break;
case 6:
if (flag)
str = "十";
else
str = "十万";
break;
case 7:
if (flag)
str = "百";
else
str = "百万";
break;
case 8:
if (flag)
str = "千";
else
str = "千万";
break;
case 9:
str = "亿";
break;
}
return str;
}
}