黑马程序员技术交流社区
标题:
C#中转换问题
[打印本页]
作者:
李玲
时间:
2012-6-11 10:35
标题:
C#中转换问题
用C#如何将数字显示改为文件显示,也就是a为1,则显示ONE,a为6,则显示SIX,a为11,则显示ELEVEN,一直显示到100为止。
还请各位能够指点一下。
作者:
刘豪
时间:
2012-6-11 10:40
可以建立一个字符串数组啊,顺序存放ONE,TWO,THERE......,a作为下标,每次输出a对应的字符串就可以了。
作者:
黑马龙超
时间:
2012-6-11 12:11
static string Function(int a)
{
string[] strs = { "one","two","three","four","five","six","seven","eight","nine","ten"};
return string.Format(strs[a-1]);
}
复制代码
作者:
吴伟烈
时间:
2012-6-19 21:29
方法很多种
1 、建立两个对应的字符串数组,同上
2、switch语句,不过效率低
3、可以用范型容器,根据Key取出Value
作者:
戴伟
时间:
2012-6-19 23:50
方法很多,可以用字典、哈希表和枚举
作者:
王志波
时间:
2012-6-25 19:24
string[] enumber={"one","two"....};
int a=convert.toint32(console.readine());
console.writeline(string.format("您输入了{0}",enumber[a-1]));
console.readkey();
没测试,不知道对不对
作者:
xiaozehope
时间:
2012-6-30 22:20
方法有很多种的,可以用数组:如:{“one”,"two","three"……}用下标取值。
可以用枚举 public enum date{one,two,three,……}
可以用字典,字典要用key来取出对应的value
作者:
戴伟
时间:
2012-7-1 10:03
可以用字符串数组、枚举、哈希表、字典,但是对应的one、two这些还得你自己敲出来的~!
作者:
古古头
时间:
2012-7-3 16:13
你需要转换的数字比较大,100以内的,不能全凭手写,我给你写一个静态类,作为工具类吧。能够装换所有的数字。
public static class NumToStr
{
private static readonly string MINUS = "minus";
private static readonly string[] X_TEN =
{
"one", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred"
};
private static readonly string[] DIGIT_STR = { "", "thousand", "million", "billion" };
private static readonly int[] DIGIT_NUM = { 1, 1000, 1000000, 1000000000 };
private static readonly string[] BASE =
{
"zero","one","two","three","four","five","six","seven","eight","nine","ten",
"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty"
};
private static string LessThan1000(int num)
{
String result = "";
Lable_Begin:
if (num < 21)
{
result += BASE[num];
}
else if (num < 100)
{
result += X_TEN[num / 10];
if ((num %= 10) != 0)
{
result += "-";
goto Lable_Begin;
}
}
else
{
result += BASE[num / 100] + " " + X_TEN[10];
if ((num %= 100) != 0)
{
result += " and ";
goto Lable_Begin;
}
}
return result;
}
public static string Convert(int num)
{
if (num == 0) return BASE[0];
int abs = Math.Abs(num);
StringBuilder result = new StringBuilder(16);
for (int i = DIGIT_NUM.Length - 1; i > -1; i--)
{
int l = abs / DIGIT_NUM[i];
if (l > 0)
{
if (result.Length > 0) result.Append(' ');
result.Append(LessThan1000(l));
if (DIGIT_STR[i] != String.Empty) result.Append(' ');
result.Append(DIGIT_STR[i]);
}
abs %= DIGIT_NUM[i];
}
return (num < 0 ? MINUS + " " : "") + result.ToString();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2