C# 小写金额替换成大写金额
找是找了一写代码!
但是大部分是javascript,VB。NET和其他语言的
我想求一段C#的 谢谢`!
用在WINFROM 的 财务核算 那!!!
[解决办法]
小写金额转化成大写金额(c#)
private readonly string _NumChinese= "零壹贰叁肆伍陆柒捌玖 ";
private readonly string _UnitChinese= "仟佰拾亿仟佰拾万仟佰拾圆角分 ";
/// <summary>
/// 小写金额转化成大写金额(只支持精确到分的小写金额)
/// </summary>
/// <returns> </returns>
public string NumCHN(double Num)
{
if(Num <=0)return String.Empty;
string sNum=Num.ToString( "F ");
if(sNum.IndexOf( ". ") <0)//对小数位的处理
sNum+= "00 ";
else
sNum=sNum.Replace( ". ",String.Empty);
string sNumCHN=String.Empty;
if(sNum.Length> _UnitChinese.Length)
throw new Exception( "数值太大,无法转化! ");
int nPosition=_UnitChinese.Length-sNum.Length;
for(int i=0;i <sNum.Length;i++)
{
string cNum=sNum.Substring(i,1);
for(int j=0;j <10;j++)
{
if(Int32.Parse(cNum)==j)
{
if(Int32.Parse(cNum)==0)
{
if(sNumCHN.LastIndexOf( "零 ")==sNumCHN.Length-1)
break;
else
{
sNumCHN+=_NumChinese.Substring(j,1);
break;
}
}
sNumCHN+=_NumChinese.Substring(j,1);
sNumCHN+=_UnitChinese.Substring(nPosition,1);
}
}
nPosition++;
}
if(sNumCHN.LastIndexOf( "零 ")==sNumCHN.Length-1)sNumCHN=sNumCHN.Substring(0,sNumCHN.Length-1);
if(sNumCHN.IndexOf( "角 ") <0 && sNumCHN.IndexOf( "分 ") <0)sNumCHN+= "整 ";
return sNumCHN;
}
[解决办法]
public class Money
{
public Money(double theMoney)
{
this.m_thedMoney = theMoney;
}
public Money(decimal theMoney)
{
//
// TODO: 在此处添加构造函数逻辑
//
this.m_theMoney = theMoney;
}
private decimal m_theMoney;
private double m_thedMoney;
/// <summary>
/// 获得decimal类型数字的中文大写形势
/// </summary>
/// <param name= "m_theMoney "> decimal类型数字 </param>
/// <returns> 返回string类型的数字的中文大写形势 </returns>
public string CmycurD()
{
string str1 = "零壹贰叁肆伍陆柒捌玖 "; //0-9所对应的汉字
string str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分 "; //数字位所对应的汉字
string str3 = " "; //从原m_theMoney值中取出的值
string str4 = " "; //数字的字符串形式
string str5 = " "; //人民币大写金额形式
int i; //循环变量
int j; //m_theMoney的值乘以100的字符串长度
string ch1 = " "; //数字的汉语读法
string ch2 = " "; //数字位的汉字读法
int nzero = 0; //用来计算连续的零值是几个
int temp; //从原m_theMoney值中取出的值
m_theMoney = Math.Round(Math.Abs(m_theMoney),2); //将m_theMoney取绝对值并四舍五入取2位小数
str4 = ((long)(m_theMoney*100)).ToString(); //将m_theMoney乘100并转换成字符串形式
j = str4.Length; //找出最高位
if (j > 15){return "溢出 ";}
str2 = str2.Substring(15-j); //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分
//循环取出每一位需要转换的值
for(i=0;i <j;i++)
{
str3 = str4.Substring(i,1); //取出需转换的某一位的值
temp = Convert.ToInt32(str3); //转换为数字
if (i != (j-3) && i != (j-7) && i != (j-11) && i != (j-15))
{
//当所取位数不为元、万、亿、万亿上的数字时
if (str3 == "0 ")
{
ch1 = " ";
ch2 = " ";
nzero = nzero + 1;
}
else
{
if(str3 != "0 " && nzero != 0)
{
ch1 = "零 " + str1.Substring(temp*1,1);
ch2 = str2.Substring(i,1);
nzero = 0;
}
else
{
ch1 = str1.Substring(temp*1,1);
ch2 = str2.Substring(i,1);
nzero = 0;
}
}
}
else
{
//该位是万亿,亿,万,元位等关键位
if (str3 != "0 " && nzero != 0)
{
ch1 = "零 " + str1.Substring(temp*1,1);
ch2 = str2.Substring(i,1);
nzero = 0;
}
else
{
if (str3 != "0 " && nzero == 0)
{
ch1 = str1.Substring(temp*1,1);
ch2 = str2.Substring(i,1);
nzero = 0;
}
else
{
if (str3 == "0 " && nzero > = 3)
{
ch1 = " ";
ch2 = " ";
nzero = nzero + 1;
}
else
{
if (j > = 11)
{
ch1 = " ";
nzero = nzero + 1;
}
else
{
ch1 = " ";
ch2 = str2.Substring(i,1);
nzero = nzero + 1;
}
}
}
}
}
if (i == (j-11) || i == (j-3))
{
//如果该位是亿位或元位,则必须写上
ch2 = str2.Substring(i,1);
}
str5 = str5 + ch1 + ch2;
if (i == j-1 && str3 == "0 " )
{
//最后一位(分)为0时,加上“整”
str5 = str5 + '整 ';
}
}
if (m_theMoney == 0)
{
str5 = "零元整 ";
}
return str5;
}
/// <summary>
/// 根据钱数变换字体颜色
/// </summary>
/// <param name= "theMoney "> 钱数 </param>
/// <param name= "dotLong "> 小数后几位 </param>
/// <param name= "moneyType "> InCome或者Expenses </param>
/// <returns> </returns>
public string IncomeAndExpensesString(int dotLong,string moneyType)
{
string returnString = String.Empty;
double returnMoney = Math.Abs(this.m_thedMoney);
RegionInfo rTemp = new RegionInfo( "CN ");
switch(dotLong)
{
case 2:
string theColor = this.GetStringColor(moneyType);
returnString = " <font color= ' "+theColor+ " '> "+returnMoney.ToString(rTemp.CurrencySymbol+ "0.00 ")+ " </font> ";
break;
default:
returnString = this.m_thedMoney.ToString();
break;
}
return returnString;
}
private string GetStringColor(string moneyType)
{
string returnString = String.Empty;
if(this.m_thedMoney <0)
{
returnString = "red ";
}
else if(this.m_thedMoney==0)
{
returnString = "black ";
}
else if(moneyType== "InCome ")
{
returnString = "green ";
}
else if(moneyType == "Expenses ")
{
returnString = "red ";
}
else if(moneyType == String.Empty || moneyType == "Default ")
{
if(this.m_thedMoney> 0)
{
returnString = "green ";
}
else if(this.m_thedMoney==0)
{
returnString = "black ";
}
else
{
returnString = "red ";
}
}
return returnString;
}
}
[解决办法]
string Num = txtinput.Text.ToString();
string[] part = Num.Split( '. ');
//part[0]是小数点前面的数字,part[1]是小数点后面的数字
int len = Num.Length;
int i=1;
string[] dw2 = { " ", "万 ", "亿 "};//大单位
string[] dw1 = { "拾 ", "佰 ", "千 "};//小单位
string[] dw = { " ", "壹 ", "贰 ", "叁 ", "肆 ", "伍 ", "陆 ", "柒 ", "捌 ", "玖 "};//整数部分用
string[] dws = { "零 ", "壹 ", "贰 ", "叁 ", "肆 ", "伍 ", "陆 ", "柒 ", "捌 ", "玖 "};//小数部分用
int k1 = 0;//计小单位
int k2 = 0;//计大单位
string str = " ";
for(i=1;i <len;i++)
{
int n = Num.Substr((len-i),1);
if( n == "0 " )
{
if ( k1 !=0 )
str = str.Substr(1,str.Length-1);
}
str = dw[Convert.ToInt32(n)] + str;//加数字
}
if(len-i-1> =0)//在数字范围内
{
if(k1!=3)//加小单位
{
str = dw1[k1] + str;
k1++;
}
else//不加小单位,加大单位
{
k1 = 0;
string temp = str.Substr(0,1);
if(temp == "万 " || temp == "亿 ")//若大单位前没有数字则舍去大单位
str = str.Substr(1,str.Length-1);
str = dw2[k2] + str;
}
}
if(k1==3)//小单位到千则大单位进一
{
k2++;
}
string strdig = " ";
//小数点后面的数
if(Num.IndexOf( ". ")!=-1)
{
if(part[1].Length > 2)
part[1] = part[1].Substring(0,2);
for( i=0;i <part[1].Length-1;i++)
{
int n = part[1].Substr(i,1);
strdig = strdig + dws[n];//加数字
}
str += "点 " + strdig;
}
txtoutput.Text = str;
[解决办法]
//转换数字
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;
}
public 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;
}