C#,阿拉伯数字转换为中文大写!!??(高分求教)

weini928 2007-06-24 03:27:53
求将阿拉伯数字转换为中文大写的C#代码,例如:12345---壹万贰千叁佰肆拾伍元整,1002.5---壹千零贰元伍角。
各位大虾请教教我……
...全文
1254 点赞 收藏 16
写回复
16 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
crossrowman 2007-06-26
string []A=new string[]{"零","一","二","三","四","五","六","七","八","九"};
string []B=new string[]{"分","角","圆","十","百","千","万","十","百","千","亿","十","百","千"};

double Val = 113465.98;
int X =(int)( Val * 100 );
string ChnStr ="";

int index=0;

while(X>0)
{
int v = X%10;
ChnStr = A[v]+B[index++]+ChnStr;
X = X/10;
}
Console.WriteLine( ChnStr );
回复
gzw323 2007-06-26
顶帖 别沉了

个人签名~~
---------------------------
惊爆支持ASP、ASP.NET2.0空间500M+SQL数据库100M 特惠价格:128一年
支持asp 300M 虚拟主机68一年
支持asp.net2.0 300M 虚拟主机88一年,快抢拉~~~~~
虚拟主机 网站空间 域名注册 主机托管 免费主机 免费空间 免费asp空间 免费虚拟主机
免费试用~~~


href="http://www.myidc.info/webhost/stylehost.aspx
硬件配置图:
http://www.myidc.info/images/adyj.gif
回复
jwind 2007-06-26
搂主好好翻翻,C语言设计教程 ,基本每个语言基础教称都会讲到 CHAR和INT是怎么转换的
回复
luhongming 2007-06-26
我去年去一家公司面试,就出了这个题,我不到一个小时搞定的。
回复
netusemaster 2007-06-25
Mark
回复
自作多情
回复
shinaterry 2007-06-24
这些。网上一搜一大堆。。。
回复
LeoMaya 2007-06-24
http://blog.csdn.net/leomaya/archive/2006/12/24/1458747.aspx
回复
nattystyle 2007-06-24
昨天刚写过,今天又有人问
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication44
{
class Program
{
static string transfer(string str)
{
string ChnNames = "零一二三四五六七八九";
string NumNames = "0123456789";
return Convert.ToString(ChnNames[NumNames.IndexOf(str)]);
}
static void Main(string[] args)
{
Console.WriteLine("请输入一个数字:");
string str = Console.ReadLine();
int weishu = str.Length;
String[] danwei ={ "元", "拾", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };
Console.WriteLine("转换后的结果是:");
int index = 0;
string output = "";
bool flag = false;
//生成转换后的结果
for (int i = str.Length; i > 0; i--)
{
index = str.Length - i;
output = output + transfer(str.Substring(index, 1));
if (str.Substring(index, 1) != "0" || i == 5 || i == 9 )
try
{
output = output + danwei[i - 1];
}
catch (Exception)
{
flag = true;
}
}
//把中间有2处或者2处以上连续0的地方给删除了
output = System.Text.RegularExpressions.Regex.Replace(output,@"(.)\1+", "$1");
//万和亿前面的“零”去除
for (int j = 0; j < output.Length - 1;j++ )
{
if (output.Substring(j, 1) == "零" && (output.Substring(j + 1, 1) == "万" || output.Substring(j + 1, 1) == "亿"))
output = output.Remove(j, 1);
}
//最末尾的“零”去除,并加上“元整”
if (output.Substring(output.Length - 1, 1) == "零")
{
output = output.Remove(output.Length - 1, 1);
output = output + "元整";
}
if (flag)
Console.WriteLine("兄弟,输入个小点的数不行么,我念不来额");
else
{
Console.WriteLine(output);
Console.WriteLine("\n故意停在这让你看结果");
}
Console.ReadLine();
}
}
}
回复
lovingkiss 2007-06-24
来源
http://www.chenjiliang.com/Article/View.aspx?ArticleID=731&TypeID=34

===================================================
技术交流不该有界限 资源共享不该有条件
博客空间:http://blog.csdn.net/lovingkiss
资源下载:http://download.csdn.net/user/lovingkiss
Email:loving-kiss@163.com
本人说明:<我的帖子我做主,结贴率保持100%>
1、欢迎一切问题有关的交流——无论答案对错;
2、不欢迎 顶、Mark、支持之类口水混分的人;
我保留对非<散分贴>蹭分者的厌恶和鄙视...
精通:jīnɡtōnɡ对学问技术等透彻的了解并熟练掌握
所以,我没有精通,只有JZ
===================================================
回复
Avoid 2007-06-24
http://topic.csdn.net/t/20040814/12/3273948.html
回复
lovingkiss 2007-06-24

private string NumToChn(string x)
{
if (x.Length == 0)
{
return "";
}
string ret = "";
if (x[0] == '-')
{
ret = "负";
x = x.Remove(0, 1);
}
if (x[0].ToString() == ".")
{
x = "0" + x;
}
if (x[x.Length - 1].ToString() == ".")
{
x = x.Remove(x.Length - 1, 1);
}
if (x.IndexOf(".") > -1)
{
ret += StrToInt(x.Substring(0, x.IndexOf("."))) + "点" + StrToDouble(x.Substring(x.IndexOf(".") + 1));
}
else
{
ret += StrToInt(x);
}
return ret;
}


private string GetMoneyChinese(Double Money)
{
int i;
string mstrSource;

if (Money == 0)
{
return "";
}
mstrSource = Money.ToString("#0.00");
i = mstrSource.IndexOf(".");
if (i > 0) { mstrSource = mstrSource.Replace(".", ""); }
if (mstrSource.Substring(0, 1) == "0") { mstrSource = mstrSource.Remove(0, 1); }

mstrSource = NumstrToChinese(mstrSource);
if (mstrSource.Length == 0) { return ""; }
//负
if (Money < 0)
{
mstrSource = "负" + mstrSource;
}

mstrSource = mstrSource.Replace("0", "零");
mstrSource = mstrSource.Replace("1", "壹");
mstrSource = mstrSource.Replace("2", "贰");
mstrSource = mstrSource.Replace("3", "叁");
mstrSource = mstrSource.Replace("4", "肆");
mstrSource = mstrSource.Replace("5", "伍");
mstrSource = mstrSource.Replace("6", "陆");
mstrSource = mstrSource.Replace("7", "柒");
mstrSource = mstrSource.Replace("8", "捌");
mstrSource = mstrSource.Replace("9", "玖");
mstrSource = mstrSource.Replace("M", "亿");
mstrSource = mstrSource.Replace("W", "万");
mstrSource = mstrSource.Replace("S", "仟");
mstrSource = mstrSource.Replace("H", "佰");
mstrSource = mstrSource.Replace("T", "拾");
mstrSource = mstrSource.Replace("Y", "圆");
mstrSource = mstrSource.Replace("J", "角");
mstrSource = mstrSource.Replace("F", "分");
if (mstrSource.Substring(mstrSource.Length - 1, 1) != "分")
{
mstrSource = mstrSource + "整";
}
return mstrSource;
}

//金额转换
private string NumstrToChinese(string numstr)
{
int i;
int j;
string mstrChar;
string[] mstrFlag = new string[4];
string mstrReturn = "";
bool mblnAddzero = false;

mstrFlag[0] = "";
mstrFlag[1] = "T";
mstrFlag[2] = "H";
mstrFlag[3] = "S";

for (i = 1; i <= numstr.Length; i++)
{
j = numstr.Length - i;
mstrChar = numstr.Substring(i - 1, 1);
if (mstrChar != "0" && j > 1) { mstrReturn = mstrReturn + mstrChar + mstrFlag[(j - 2) % 4]; }
if (mstrChar == "0" && mblnAddzero == false)
{
mstrReturn = mstrReturn + "0";
mblnAddzero = true;
}
if (j == 14)
{
if (mstrReturn.Substring(mstrReturn.Length - 1) == "0")
{ mstrReturn = mstrReturn.Substring(0, mstrReturn.Length - 1) + "W0"; }
else
{ mstrReturn = mstrReturn + "W"; }
}
if (j == 2)
{
if (mstrReturn.Substring(mstrReturn.Length - 1, 1) == "0")
{ mstrReturn = mstrReturn.Substring(0, mstrReturn.Length - 1) + "Y0"; }
else
{ mstrReturn = mstrReturn + "Y"; }
//元
}
if (j == 6)
{
if (mstrReturn.Length > 2)
{
if (mstrReturn.Substring(mstrReturn.Length - 2) != "M0")
{
if (mstrReturn.Substring(mstrReturn.Length - 1) == "0")
{ mstrReturn = mstrReturn.Substring(0, mstrReturn.Length - 1) + "W0"; }
else
{ mstrReturn = mstrReturn + "W"; }
}
}
else
{
if (mstrReturn.Substring(mstrReturn.Length - 1) == "0")
{ mstrReturn = mstrReturn.Substring(0, mstrReturn.Length - 1) + "W0"; }
else
{ mstrReturn = mstrReturn + "W"; }
}
}
if (j == 10)
{
if (mstrReturn.Substring(mstrReturn.Length - 1) == "0")
{ mstrReturn = mstrReturn.Substring(0, mstrReturn.Length - 1) + "M0"; }
else
{ mstrReturn = mstrReturn + "M"; }
}
if (j == 0 && mstrChar != "0") { mstrReturn = mstrReturn + mstrChar + "F"; }
if (j == 1 && mstrChar != "0") { mstrReturn = mstrReturn + mstrChar + "J"; }
if (mstrChar != "0") { mblnAddzero = false; }
}
if (mstrReturn.Substring(0, 1) == "1" && mstrReturn.Substring(1, 1) == mstrFlag[1]) { mstrReturn = mstrReturn.Substring(1); }
if (mstrReturn.Substring(mstrReturn.Length - 1, 1) == "0") { mstrReturn = mstrReturn.Substring(0, mstrReturn.Length - 1); }
if (mstrReturn.Substring(0, 1) == "0") { mstrReturn = mstrReturn.Substring(1); }
if (mstrReturn.Substring(mstrReturn.Length - 1, 1) == "M" || mstrReturn.Substring(mstrReturn.Length - 1, 1) == "W" || mstrReturn.Substring(mstrReturn.Length - 1, 1) == "S" || mstrReturn.Substring(mstrReturn.Length - 1, 1) == "H" || mstrReturn.Substring(mstrReturn.Length - 1, 1) == "T") { mstrReturn = mstrReturn + "Y"; }
return mstrReturn;
}


}
}
回复
lovingkiss 2007-06-24
来源
http://www.chenjiliang.com/Article/View.aspx?ArticleID=731&TypeID=34
using System;

namespace TestUpperRMB
{
/// <summary>
/// 本类实现阿拉伯数字到大写中文的转换
/// 该类没有对非法数字进行判别,请事先自己判断数字是否合法
/// </summary>
public class ChineseNum
{
public static string GetChineseNum(string p_num)
{
ChineseNum cn = new ChineseNum();

return cn.NumToChn(p_num);
}

public static string GetUpperMoney(double p_Money)
{
ChineseNum cn = new ChineseNum();

return cn.GetMoneyChinese(p_Money);
}

//转换数字
private char CharToNum(char x)
{
string stringChnNames = "零一二三四五六七八九";
string stringNumNames = "0123456789";
return stringChnNames[stringNumNames.IndexOf(x)];
}

//转换万以下整数
private string WanStrToInt(string x)
{
string[] stringArrayLevelNames = new string[4] { "", "十", "百", "千" };
string ret = "";
int i;
for (i = x.Length - 1; i >= 0; i--)
if (x[i] == '0')
{
ret = CharToNum(x[i]) + ret;
}
else
{
ret = CharToNum(x[i]) + stringArrayLevelNames[x.Length - 1 - i] + ret;
}
while ((i = ret.IndexOf("零零")) != -1)
{
ret = ret.Remove(i, 1);
}
if (ret[ret.Length - 1] == '零' && ret.Length > 1)
{
ret = ret.Remove(ret.Length - 1, 1);
}
if (ret.Length >= 2 && ret.Substring(0, 2) == "一十")
{
ret = ret.Remove(0, 1);
}
return ret;
}
//转换整数
private string StrToInt(string x)
{
int len = x.Length;
string ret, temp;
if (len <= 4)
{
ret = WanStrToInt(x);
}
else if (len <= 8)
{
ret = WanStrToInt(x.Substring(0, len - 4)) + "万";
temp = WanStrToInt(x.Substring(len - 4, 4));
if (temp.IndexOf("千") == -1 && temp != "")
ret += "零" + temp;
else
ret += temp;
}
else
{
ret = WanStrToInt(x.Substring(0, len - 8)) + "亿";
temp = WanStrToInt(x.Substring(len - 8, 4));
if (temp.IndexOf("千") == -1 && temp != "")
{
ret += "零" + temp;
}
else
{
ret += temp;
}
ret += "万";
temp = WanStrToInt(x.Substring(len - 4, 4));
if (temp.IndexOf("千") == -1 && temp != "")
{
ret += "零" + temp;
}
else
{
ret += temp;
}

}
int i;
if ((i = ret.IndexOf("零万")) != -1)
{
ret = ret.Remove(i + 1, 1);
}
while ((i = ret.IndexOf("零零")) != -1)
{
ret = ret.Remove(i, 1);
}
if (ret[ret.Length - 1] == '零' && ret.Length > 1)
{
ret = ret.Remove(ret.Length - 1, 1);
}
return ret;
}
//转换小数
private string StrToDouble(string x)
{
string ret = "";
for (int i = 0; i < x.Length; i++)
{
ret += CharToNum(x[i]);
}
return ret;
}
回复
Avoid 2007-06-24
//作者:fanz2000
//Email:fanz2000@sohu.com
/// <summary>
/// 转换数字金额主函数(包括小数)
/// </summary>
/// <param name="str">数字字符串</param>
/// <returns>转换成中文大写后的字符串或者出错信息提示字符串</returns>
public string ConvertSum(string str)
{
if(!IsPositveDecimal(str))
return "输入的不是正数字!";
if(Double.Parse(str)>999999999999.99)
return "数字太大,无法换算,请输入一万亿元以下的金额";
char[] ch=new char[1];
ch[0]='.'; //小数点
string[] splitstr=null; //定义按小数点分割后的字符串数组
splitstr=str.Split(ch[0]);//按小数点分割字符串
if(splitstr.Length==1) //只有整数部分
return ConvertData(str)+"圆整";
else //有小数部分
{
string rstr;
rstr=ConvertData(splitstr[0])+"圆";//转换整数部分
rstr+=ConvertXiaoShu(splitstr[1]);//转换小数部分
return rstr;
}

}
/// <summary>
/// 判断是否是正数字字符串
/// </summary>
/// <param name="str"> 判断字符串</param>
/// <returns>如果是数字,返回true,否则返回false</returns>
public bool IsPositveDecimal(string str)
{
Decimal d;
try
{
d=Decimal.Parse(str);

}
catch(Exception)
{
return false;
}
if(d>0)
return true;
else
return false;
}
/// <summary>
/// 转换数字(整数)
/// </summary>
/// <param name="str">需要转换的整数数字字符串</param>
/// <returns>转换成中文大写后的字符串</returns>
public string ConvertData(string str)
{
string tmpstr="";
string rstr="";
int strlen=str.Length;
if (strlen<=4)//数字长度小于四位
{
rstr= ConvertDigit(str);

}
else
{

if (strlen<=8)//数字长度大于四位,小于八位
{
tmpstr=str.Substring(strlen-4,4);//先截取最后四位数字
rstr=ConvertDigit(tmpstr);//转换最后四位数字
tmpstr=str.Substring(0,strlen-4);//截取其余数字
//将两次转换的数字加上萬后相连接
rstr= String.Concat(ConvertDigit(tmpstr)+"萬",rstr);
rstr=rstr.Replace("零萬","萬");
rstr=rstr.Replace("零零","零");

}
else
if(strlen<=12)//数字长度大于八位,小于十二位
{
tmpstr=str.Substring(strlen-4,4);//先截取最后四位数字
rstr=ConvertDigit(tmpstr);//转换最后四位数字
tmpstr=str.Substring(strlen-8,4);//再截取四位数字
rstr= String.Concat(ConvertDigit(tmpstr)+"萬",rstr);
tmpstr=str.Substring(0,strlen-8);
rstr= String.Concat(ConvertDigit(tmpstr)+"億",rstr);
rstr=rstr.Replace("零億","億");
rstr=rstr.Replace("零萬","零");
rstr=rstr.Replace("零零","零");
rstr=rstr.Replace("零零","零");
}
}
strlen=rstr.Length;
if (strlen>=2)
{
switch(rstr.Substring(strlen-2,2))
{
case "佰零":rstr=rstr.Substring(0,strlen-2)+"佰"; break;
case "仟零":rstr=rstr.Substring(0,strlen-2)+"仟"; break;
case "萬零":rstr=rstr.Substring(0,strlen-2)+"萬";break;
case "億零":rstr=rstr.Substring(0,strlen-2)+"億";break;

}
}

return rstr;
}
/// <summary>
/// 转换数字(小数部分)
/// </summary>
/// <param name="str">需要转换的小数部分数字字符串</param>
/// <returns>转换成中文大写后的字符串</returns>
public string ConvertXiaoShu(string str)
{
int strlen=str.Length ;
string rstr;
if(strlen==1)
{
rstr=ConvertChinese(str)+"角";
return rstr;
}
else
{
string tmpstr=str.Substring(0,1);
rstr=ConvertChinese(tmpstr)+"角";
tmpstr=str.Substring(1,1);
rstr+=ConvertChinese(tmpstr)+"分";
rstr=rstr.Replace("零分","");
rstr=rstr.Replace("零角","");
return rstr;
}


}

/// <summary>
/// 转换数字
/// </summary>
/// <param name="str">转换的字符串(四位以内)</param>
/// <returns></returns>
public string ConvertDigit(string str)
{
int strlen=str.Length;
string rstr="";
switch (strlen)
{
case 1: rstr=ConvertChinese(str);break;
case 2: rstr=Convert2Digit(str);break;
case 3: rstr=Convert3Digit(str);break;
case 4: rstr=Convert4Digit(str);break;
}
rstr=rstr.Replace("拾零","拾");
strlen=rstr.Length;

return rstr;
}


/// <summary>
/// 转换四位数字
/// </summary>
public string Convert4Digit(string str)
{
string str1=str.Substring(0,1);
string str2=str.Substring(1,1);
string str3=str.Substring(2,1);
string str4=str.Substring(3,1);
string rstring="";
rstring+=ConvertChinese(str1)+"仟";
rstring+=ConvertChinese(str2)+"佰";
rstring+=ConvertChinese(str3)+"拾";
rstring+=ConvertChinese(str4);
rstring=rstring.Replace("零仟","零");
rstring=rstring.Replace("零佰","零");
rstring=rstring.Replace("零拾","零");
rstring=rstring.Replace("零零","零");
rstring=rstring.Replace("零零","零");
rstring=rstring.Replace("零零","零");
return rstring;
}
/// <summary>
/// 转换三位数字
/// </summary>
public string Convert3Digit(string str)
{
string str1=str.Substring(0,1);
string str2=str.Substring(1,1);
string str3=str.Substring(2,1);
string rstring="";
rstring+=ConvertChinese(str1)+"佰";
rstring+=ConvertChinese(str2)+"拾";
rstring+=ConvertChinese(str3);
rstring=rstring.Replace("零佰","零");
rstring=rstring.Replace("零拾","零");
rstring=rstring.Replace("零零","零");
rstring=rstring.Replace("零零","零");
return rstring;
}
/// <summary>
/// 转换二位数字
/// </summary>
public string Convert2Digit(string str)
{
string str1=str.Substring(0,1);
string str2=str.Substring(1,1);
string rstring="";
rstring+=ConvertChinese(str1)+"拾";
rstring+=ConvertChinese(str2);
rstring=rstring.Replace("零拾","零");
rstring=rstring.Replace("零零","零");
return rstring;
}
/// <summary>
/// 将一位数字转换成中文大写数字
/// </summary>
public string ConvertChinese(string str)
{
//"零壹贰叁肆伍陆柒捌玖拾佰仟萬億圆整角分"
string cstr="";
switch(str)
{
case "0": cstr="零";break;
case "1": cstr="壹";break;
case "2": cstr="贰";break;
case "3": cstr="叁";break;
case "4": cstr="肆";break;
case "5": cstr="伍";break;
case "6": cstr="陆";break;
case "7": cstr="柒";break;
case "8": cstr="捌";break;
case "9": cstr="玖";break;
}
return(cstr);
}
回复
今天结贴今天答
回复
-过客- 2007-06-24
参考

http://www.chenjiliang.com/Article/View.aspx?ArticleID=732&TypeID=34
http://www.chenjiliang.com/Article/View.aspx?ArticleID=731&TypeID=34
http://www.chenjiliang.com/Article/View.aspx?ArticleID=668&TypeID=34
回复
发帖
C#
创建于2007-09-28

10.6w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2007-06-24 03:27
社区公告

让您成为最强悍的C#开发者