金额阿拉伯数字转换为中文的存储过程
Create Procedure AtoC
@ChangeMoney Money
as
Set Nocount ON
Declare @String1 char(20)
Declare @String2 char(30)
Declare @String4 Varchar(100)
Declare @String3 Varchar(100) --从原A值中取出的值
Declare @i int --循环变量
Declare @J Int --A的值乘以100的字符串长度
Declare @Ch1 Varchar(100) --数字的汉语读法
Declare @Ch2 Varchar(100) --数字位的汉字读法
Declare @Zero Int --用来计算连续有几个零
Declare @ReturnValue VarChar(100)
Create Procedure AtoC
@ChangeMoney Money
as
Set Nocount ON
Declare @String1 char(20)
Declare @String2 char(30)
Declare @String4 Varchar(100)
Declare @String3 Varchar(100) --从原A值中取出的值
Declare @i int --循环变量
Declare @J Int --A的值乘以100的字符串长度
Declare @Ch1 Varchar(100) --数字的汉语读法
Declare @Ch2 Varchar(100) --数字位的汉字读法
Declare @Zero Int --用来计算连续有几个零
Declare @ReturnValue VarChar(100)
Select @ReturnValue = ''
Select @String1 = '零壹贰叁肆伍陆柒捌玖'
Select @String2 = '万仟佰拾亿仟佰拾万仟佰拾元角分'
Select @String4 = Cast(@ChangeMoney*100 as int)
select @J=len(cast((@ChangeMoney*100) as int))
Select @String2=Right(@String2,@J)
Select @i = 1
while @i<= @j Begin
Select @String3 = Substring(@String4,@i,1)
if @String3<>'0' Begin
Select @Ch1 = Substring(@String1, Cast(@String3 as Int) + 1, 1)
Select @Ch2 = Substring(@String2, @i, 1)
Select @Zero = 0 --表示本位不为零
end
else Begin
If (@Zero = 0) Or (@i = @J - 9) Or (@i = @J - 5) Or (@i = @J - 1)
Select @Ch1 = '零'
Else
Select @Ch1 = ''
Select @Zero = @Zero + 1 --表示本位为0
if @String3<>'0' Begin
Select @Ch1 = Substring(@String1, Cast(@String3 as Int) + 1, 1)
Select @Ch2 = Substring(@String2, @i, 1)
Select @Zero = 0 --表示本位不为零
end
else Begin
If (@Zero = 0) Or (@i = @J - 9) Or (@i = @J - 5) Or (@i = @J - 1)
Select @Ch1 = '零'
Else
Select @Ch1 = ''
Select @Zero = @Zero + 1 --表示本位为0
--如果转换的数值需要扩大,那么需改动以下表达式 I 的值。
Select Ch2 = ''
If @i = @J - 10 Begin
Select @Ch2 = '亿'
Select @Zero = 0
end
If @i = @J - 6 Begin
Select @Ch2 = '万'
Select @Zero = 0
end
if @i = @J - 2 Begin
Select @Ch2 = '元'
Select @Zero = 0
end
If @i = @J
Select @Ch2 = '整'
end
Select @ReturnValue = @ReturnValue + @Ch1 + @Ch2
select @i = @i+1
end
--最后将多余的零去掉
Select @Zero = 0
end
If @i = @J
Select @Ch2 = '整'
end
Select @ReturnValue = @ReturnValue + @Ch1 + @Ch2
select @i = @i+1
end
--最后将多余的零去掉
If CharIndex('仟仟',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '仟仟', '仟')
If CharIndex('佰佰',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '佰佰', '佰')
If CharIndex('零元',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零元', '元')
If CharIndex('零万',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零万', '万')
If CharIndex('零亿',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零亿', '亿')
Select @ReturnValue = Replace(@ReturnValue, '仟仟', '仟')
If CharIndex('佰佰',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '佰佰', '佰')
If CharIndex('零元',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零元', '元')
If CharIndex('零万',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零万', '万')
If CharIndex('零亿',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零亿', '亿')
If CharIndex('零整',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零整', '整')
If CharIndex('零佰',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零佰', '零')
If CharIndex('零仟',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零仟', '零')
If CharIndex('元元',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '元元', '元')
Select @ReturnValue
GO
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Select @ReturnValue = Replace(@ReturnValue, '零整', '整')
If CharIndex('零佰',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零佰', '零')
If CharIndex('零仟',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零仟', '零')
If CharIndex('元元',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '元元', '元')
Select @ReturnValue
GO
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
一个将小写金额转换为大写的C#函数
//最大支持99,999,999.99元
private string ChMoney(float Money)
{
?string[] MChar={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
?string[] MUnit = {"分","角","元","拾","佰","仟","万","拾","佰","仟"};
?string strMoney = ((long)(Money * 100)).ToString();
?string outMoney = "";
?for(int i = 0;i < strMoney.Length;i++)
?{
??outMoney += MChar[int.Parse(strMoney.Substring(i,1))] + MUnit[strMoney.Length - i - 1];
?}
?return outMoney;
}
private string ChMoney(float Money)
{
?string[] MChar={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
?string[] MUnit = {"分","角","元","拾","佰","仟","万","拾","佰","仟"};
?string strMoney = ((long)(Money * 100)).ToString();
?string outMoney = "";
?for(int i = 0;i < strMoney.Length;i++)
?{
??outMoney += MChar[int.Parse(strMoney.Substring(i,1))] + MUnit[strMoney.Length - i - 1];
?}
?return outMoney;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ASP.Net fanz2000 王嘉 无 l <![CDATA[以下C#代码实现了将阿拉伯数字转换成中文大写金额的功能
//作者:fanz2000
//Email:fanz2000@sohu
///
///转换数字金额主函数(包括小数)
///
///数字字符串
///转换成中文大写后的字符串或者出错信息提示字符串
publicstringConvertSum(stringstr)
{
if(!IsPositveDecimal(str))
return"输入的不是正数字!";
if(Double.Parse(str)>999999999999.99)
//作者:fanz2000
//Email:fanz2000@sohu
///
///转换数字金额主函数(包括小数)
///
///数字字符串
///转换成中文大写后的字符串或者出错信息提示字符串
publicstringConvertSum(stringstr)
{
if(!IsPositveDecimal(str))
return"输入的不是正数字!";
if(Double.Parse(str)>999999999999.99)
return"数字太大,无法换算,请输入一万亿元以下的金额";
char[]ch=newchar[1];
ch[0]='.';//小数点
string[]splitstr=null;//定义按小数点分割后的字符串数组
splitstr=str.Split(ch[0]);//按小数点分割字符串
if(splitstr.Length==1)//只有整数部分
returnConvertData(str)+"圆整";
else//有小数部分
{
stringrstr;
rstr=ConvertData(splitstr[0])+"圆";//转换整数部分
rstr+=ConvertXiaoShu(splitstr[1]);//转换小数部分
returnrstr;
}
char[]ch=newchar[1];
ch[0]='.';//小数点
string[]splitstr=null;//定义按小数点分割后的字符串数组
splitstr=str.Split(ch[0]);//按小数点分割字符串
if(splitstr.Length==1)//只有整数部分
returnConvertData(str)+"圆整";
else//有小数部分
{
stringrstr;
rstr=ConvertData(splitstr[0])+"圆";//转换整数部分
rstr+=ConvertXiaoShu(splitstr[1]);//转换小数部分
returnrstr;
}
}
///
///判断是否是正数字字符串
///
///判断字符串
///如果是数字,返回true,否则返回false
publicboolIsPositveDecimal(stringstr)
{
Decimald;
try
{
d=Decimal.Parse(str);
}
catch(Exception)
///
///判断是否是正数字字符串
///
///判断字符串
///如果是数字,返回true,否则返回false
publicboolIsPositveDecimal(stringstr)
{
Decimald;
try
{
d=Decimal.Parse(str);
}
catch(Exception)
{
returnfalse;
}
if(d>0)
returntrue;
else
returnfalse;
}
///
///转换数字(整数)
///
///需要转换的整数数字字符串
///转换成中文大写后的字符串
publicstringConvertData(stringstr)
{
returnfalse;
}
if(d>0)
returntrue;
else
returnfalse;
}
///
///转换数字(整数)
///
///需要转换的整数数字字符串
///转换成中文大写后的字符串
publicstringConvertData(stringstr)
{
stringtmpstr="";
stringrstr="";
intstrlen=str.Length;
if(strlen<=4)//数字长度小于四位
{
rstr=ConvertDigit(str);
}
else
{
if(strlen<=8)//数字长度大于四位,小于八位
{
tmpstr=str.Substring(strlen-4,4);//先截取最后四位数字
rstr=ConvertDigit(tmpstr);//转换最后四位数字
stringrstr="";
intstrlen=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("零零","零");
}
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;
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;
}
}
returnrstr;
}
///
///转换数字(小数部分)
///
///需要转换的小数部分数字字符串
///转换成中文大写后的字符串
publicstringConvertXiaoShu(stringstr)
{
intstrlen=str.Length;
}
}
returnrstr;
}
///
///转换数字(小数部分)
///
///需要转换的小数部分数字字符串
///转换成中文大写后的字符串
publicstringConvertXiaoShu(stringstr)
{
intstrlen=str.Length;
stringrstr;
if(strlen==1)
{
rstr=ConvertChinese(str)+"角";
returnrstr;
}
else
{
stringtmpstr=str.Substring(0,1);
rstr=ConvertChinese(tmpstr)+"角";
tmpstr=str.Substring(1,1);
rstr+=ConvertChinese(tmpstr)+"分";
rstr=rstr.Replace("零分","");
rstr=rstr.Replace("零角","");
returnrstr;
if(strlen==1)
{
rstr=ConvertChinese(str)+"角";
returnrstr;
}
else
{
stringtmpstr=str.Substring(0,1);
rstr=ConvertChinese(tmpstr)+"角";
tmpstr=str.Substring(1,1);
rstr+=ConvertChinese(tmpstr)+"分";
rstr=rstr.Replace("零分","");
rstr=rstr.Replace("零角","");
returnrstr;
}
}
///
///转换数字
///
///转换的字符串(四位以内)
///
publicstringConvertDigit(stringstr)
{
intstrlen=str.Length;
stringrstr="";
switch(strlen)
}
///
///转换数字
///
///转换的字符串(四位以内)
///
publicstringConvertDigit(stringstr)
{
intstrlen=str.Length;
stringrstr="";
switch(strlen)
{
case1:rstr=ConvertChinese(str);break;
case2:rstr=Convert2Digit(str);break;
case3:rstr=Convert3Digit(str);break;
case4:rstr=Convert4Digit(str);break;
}
rstr=rstr.Replace("拾零","拾");
strlen=rstr.Length;
returnrstr;
}
///
///转换四位数字
/
case1:rstr=ConvertChinese(str);break;
case2:rstr=Convert2Digit(str);break;
case3:rstr=Convert3Digit(str);break;
case4:rstr=Convert4Digit(str);break;
}
rstr=rstr.Replace("拾零","拾");
strlen=rstr.Length;
returnrstr;
}
///
///转换四位数字
/
//
publicstringConvert4Digit(stringstr)
{
stringstr1=str.Substring(0,1);
stringstr2=str.Substring(1,1);
stringstr3=str.Substring(2,1);
stringstr4=str.Substring(3,1);
stringrstring="";
rstring+=ConvertChinese(str1)+"仟";
rstring+=ConvertChinese(str2)+"佰";
rstring+=ConvertChinese(str3)+"拾";
rstring+=ConvertChinese(str4);
rstring=rstring.Replace("零仟","零");
rstring=rstring.Replace("零佰","零");
rstring=rstring.Replace("零拾","零");
publicstringConvert4Digit(stringstr)
{
stringstr1=str.Substring(0,1);
stringstr2=str.Substring(1,1);
stringstr3=str.Substring(2,1);
stringstr4=str.Substring(3,1);
stringrstring="";
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("零零","零");
returnrstring;
}
///
///转换三位数字
///
publicstringConvert3Digit(stringstr)
{
stringstr1=str.Substring(0,1);
stringstr2=str.Substring(1,1);
stringstr3=str.Substring(2,1);
stringrstring="";
rstring+=ConvertChinese(str1)+"佰";
rstring=rstring.Replace("零零","零");
rstring=rstring.Replace("零零","零");
returnrstring;
}
///
///转换三位数字
///
publicstringConvert3Digit(stringstr)
{
stringstr1=str.Substring(0,1);
stringstr2=str.Substring(1,1);
stringstr3=str.Substring(2,1);
stringrstring="";
rstring+=ConvertChinese(str1)+"佰";
rstring+=ConvertChinese(str2)+"拾";
rstring+=ConvertChinese(str3);
rstring=rstring.Replace("零佰","零");
rstring=rstring.Replace("零拾","零");
rstring=rstring.Replace("零零","零");
rstring=rstring.Replace("零零","零");
returnrstring;
}
///
///转换二位数字
///
publicstringConvert2Digit(stringstr)
{
stringstr1=str.Substring(0,1);
stringstr2=str.Substring(1,1);
rstring+=ConvertChinese(str3);
rstring=rstring.Replace("零佰","零");
rstring=rstring.Replace("零拾","零");
rstring=rstring.Replace("零零","零");
rstring=rstring.Replace("零零","零");
returnrstring;
}
///
///转换二位数字
///
publicstringConvert2Digit(stringstr)
{
stringstr1=str.Substring(0,1);
stringstr2=str.Substring(1,1);
stringrstring="";
rstring+=ConvertChinese(str1)+"拾";
rstring+=ConvertChinese(str2);
rstring=rstring.Replace("零拾","零");
rstring=rstring.Replace("零零","零");
returnrstring;
}
///
///将一位数字转换成中文大写数字
///
publicstringConvertChinese(stringstr)
{
//"零壹贰叁肆伍陆柒捌玖拾佰仟萬億圆整角分"
stringcstr="";
switch(str)
rstring+=ConvertChinese(str1)+"拾";
rstring+=ConvertChinese(str2);
rstring=rstring.Replace("零拾","零");
rstring=rstring.Replace("零零","零");
returnrstring;
}
///
///将一位数字转换成中文大写数字
///
publicstringConvertChinese(stringstr)
{
//"零壹贰叁肆伍陆柒捌玖拾佰仟萬億圆整角分"
stringcstr="";
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);
}
]]>
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);
}
]]>
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
金额转换成大写的算法(C#) [ 2004-11-16 08:02 PM |
//名称:ConvertSignCap
//参数:number
//返回:CapNumber
//描述:将传入的单个数字转换成中文大写形式
//创建人:刘权 2004-11-16
public static string ConvertSignCap(int SignNumber)
{
string CapNumber="";
switch(SignNumber)
{
case 0:
CapNumber = "零";
break;
case 1:
CapNumber = "壹";
break;
case 2:
CapNumber = "贰";
break;
case 3:
CapNumber = "叁";
break;
case 4:
CapNumber = "肆";
break;
case 5:
break;
case 1:
CapNumber = "壹";
break;
case 2:
CapNumber = "贰";
break;
case 3:
CapNumber = "叁";
break;
case 4:
CapNumber = "肆";
break;
case 5:
CapNumber = "伍";
break;
case 6:
CapNumber = "陆";
break;
case 7:
CapNumber = "柒";
break;
case 8:
CapNumber = "捌";
break;
case 9:
CapNumber = "玖";
break;
}
break;
case 6:
CapNumber = "陆";
break;
case 7:
CapNumber = "柒";
break;
case 8:
CapNumber = "捌";
break;
case 9:
CapNumber = "玖";
break;
}
return CapNumber;
}
//名称:Symbol
//参数:NumLen,i
//返回:SymbolStr
//描述:根据传入的数值长度以及循环位数,返回其标识位
//创建人:刘权 2004-11-16
public static string Symbol(int NumLen,int i)
{
string SymbolStr = "";
switch(NumLen)
{
case 1:
SymbolStr="";
}
//名称:Symbol
//参数:NumLen,i
//返回:SymbolStr
//描述:根据传入的数值长度以及循环位数,返回其标识位
//创建人:刘权 2004-11-16
public static string Symbol(int NumLen,int i)
{
string SymbolStr = "";
switch(NumLen)
{
case 1:
SymbolStr="";
break;
case 2:
switch(i)
{
case 0:
SymbolStr="拾";
break;
case 1:
SymbolStr="";
break;
}
break;
case 3:
switch(i)
{
case 2:
switch(i)
{
case 0:
SymbolStr="拾";
break;
case 1:
SymbolStr="";
break;
}
break;
case 3:
switch(i)
{
case 0:
SymbolStr="佰";
break;
case 1:
SymbolStr="拾";
break;
case 2:
SymbolStr="";
break;
}
break;
case 4:
switch(i)
{
case 0:
SymbolStr="佰";
break;
case 1:
SymbolStr="拾";
break;
case 2:
SymbolStr="";
break;
}
break;
case 4:
switch(i)
{
case 0:
SymbolStr="仟";
break;
case 1:
SymbolStr="佰";
break;
case 2:
SymbolStr="拾";
break;
case 3:
SymbolStr="";
break;
}
break;
}
return SymbolStr;
break;
case 1:
SymbolStr="佰";
break;
case 2:
SymbolStr="拾";
break;
case 3:
SymbolStr="";
break;
}
break;
}
return SymbolStr;
}
//名称:ConvertToCap
//参数:number
//返回:CapNumber
//描述:将传入的金额转换(小数点后面保留2位小数)成中文大写形式
//创建人:刘权 2004-11-16
public static string ConvertToCap(double Number)
{
string NumberStr = Number.ToString();
if(NumberStr.IndexOf(".") <= 0)
{
NumberStr = NumberStr + ".00";
}
string CapStr = "";
string PartStr1 = "";
//名称:ConvertToCap
//参数:number
//返回:CapNumber
//描述:将传入的金额转换(小数点后面保留2位小数)成中文大写形式
//创建人:刘权 2004-11-16
public static string ConvertToCap(double Number)
{
string NumberStr = Number.ToString();
if(NumberStr.IndexOf(".") <= 0)
{
NumberStr = NumberStr + ".00";
}
string CapStr = "";
string PartStr1 = "";
string PartStr2 = "";
string[] NumArr = NumberStr.Split('.');
//整数部分
string FNumber = NumArr[0].ToString();
//小数部分
string SNumber = NumArr[1].ToString();
if(FNumber.Length > 12)
{
//不计算大于1兆的金额。
CapStr = "数值太大,不能计算...";
}
else
{
//-------1:转换整数部分。
int FNumberLen = FNumber.Length;
string[] NumArr = NumberStr.Split('.');
//整数部分
string FNumber = NumArr[0].ToString();
//小数部分
string SNumber = NumArr[1].ToString();
if(FNumber.Length > 12)
{
//不计算大于1兆的金额。
CapStr = "数值太大,不能计算...";
}
else
{
//-------1:转换整数部分。
int FNumberLen = FNumber.Length;
//万位之内
if(FNumberLen < 5)
{
string IStr1 = "";
for(int i = 0;i < FNumberLen;i++)
{
IStr1 = ConvertSignCap(int.Parse(FNumber.Substring(i,1))) + Symbol(FNumberLen,i);
PartStr1 = PartStr1==""?IStr1:PartStr1 + IStr1;
}
}
//亿位之内
if(FNumberLen > 4 && FNumberLen < 9)
{
string TempStr1 = "";
string TempStr2 = "";
if(FNumberLen < 5)
{
string IStr1 = "";
for(int i = 0;i < FNumberLen;i++)
{
IStr1 = ConvertSignCap(int.Parse(FNumber.Substring(i,1))) + Symbol(FNumberLen,i);
PartStr1 = PartStr1==""?IStr1:PartStr1 + IStr1;
}
}
//亿位之内
if(FNumberLen > 4 && FNumberLen < 9)
{
string TempStr1 = "";
string TempStr2 = "";
string IStr1 = "";
string IStr2 = "";
//将值分割成两部分:未超出万位的;超过万位
string Part1 = FNumber.Substring(FNumberLen-4,4);
string Part2 = FNumber.Substring(0,FNumberLen-4);
for(int i = 0;i < Part1.Length;i++)
{
IStr1 = ConvertSignCap(int.Parse(Part1.Substring(i,1))) + Symbol(Part1.Length,i);
TempStr1 = TempStr1==""?IStr1:(TempStr1 + IStr1);
}
for(int i = 0;i < Part2.Length;i++)
{
IStr2 = ConvertSignCap(int.Parse(Part2.Substring(i,1))) + Symbol(Part2.Length,i);
TempStr2 = TempStr2==""?IStr2:(TempStr2 + IStr2);
}
string IStr2 = "";
//将值分割成两部分:未超出万位的;超过万位
string Part1 = FNumber.Substring(FNumberLen-4,4);
string Part2 = FNumber.Substring(0,FNumberLen-4);
for(int i = 0;i < Part1.Length;i++)
{
IStr1 = ConvertSignCap(int.Parse(Part1.Substring(i,1))) + Symbol(Part1.Length,i);
TempStr1 = TempStr1==""?IStr1:(TempStr1 + IStr1);
}
for(int i = 0;i < Part2.Length;i++)
{
IStr2 = ConvertSignCap(int.Parse(Part2.Substring(i,1))) + Symbol(Part2.Length,i);
TempStr2 = TempStr2==""?IStr2:(TempStr2 + IStr2);
}
PartStr1 = TempStr2 + "万" + TempStr1;
}
//兆位之内
if(FNumberLen > 8 && FNumberLen < 13)
{
string TempStr1 = "";
string TempStr2 = "";
string TempStr3 = "";
string IStr1 = "";
string IStr2 = "";
string IStr3 = "";
//将值分割成三部分:未超出万位的;超过万位;超出亿位的
string Part1 = FNumber.Substring(FNumberLen-4,4);
string Part2 = FNumber.Substring(FNumberLen-8,4);
string Part3 = FNumber.Substring(0,FNumberLen-8);
}
//兆位之内
if(FNumberLen > 8 && FNumberLen < 13)
{
string TempStr1 = "";
string TempStr2 = "";
string TempStr3 = "";
string IStr1 = "";
string IStr2 = "";
string IStr3 = "";
//将值分割成三部分:未超出万位的;超过万位;超出亿位的
string Part1 = FNumber.Substring(FNumberLen-4,4);
string Part2 = FNumber.Substring(FNumberLen-8,4);
string Part3 = FNumber.Substring(0,FNumberLen-8);
for(int i = 0;i < Part1.Length;i++)
{
IStr1 = ConvertSignCap(int.Parse(Part1.Substring(i,1))) + Symbol(Part1.Length,i);
TempStr1 = TempStr1==""?IStr1:(TempStr1 + IStr1);
}
for(int i = 0;i < Part2.Length;i++)
{
IStr2 = ConvertSignCap(int.Parse(Part2.Substring(i,1))) + Symbol(Part2.Length,i);
TempStr2 = TempStr2==""?IStr2:(TempStr2 + IStr2);
}
for(int i = 0;i < Part3.Length;i++)
{
IStr3 = ConvertSignCap(int.Parse(Part3.Substring(i,1))) + Symbol(Part3.Length,i);
TempStr3 = TempStr3==""?IStr3:(TempStr3 + IStr3);
}
{
IStr1 = ConvertSignCap(int.Parse(Part1.Substring(i,1))) + Symbol(Part1.Length,i);
TempStr1 = TempStr1==""?IStr1:(TempStr1 + IStr1);
}
for(int i = 0;i < Part2.Length;i++)
{
IStr2 = ConvertSignCap(int.Parse(Part2.Substring(i,1))) + Symbol(Part2.Length,i);
TempStr2 = TempStr2==""?IStr2:(TempStr2 + IStr2);
}
for(int i = 0;i < Part3.Length;i++)
{
IStr3 = ConvertSignCap(int.Parse(Part3.Substring(i,1))) + Symbol(Part3.Length,i);
TempStr3 = TempStr3==""?IStr3:(TempStr3 + IStr3);
}
PartStr1 = TempStr3 + "亿" + TempStr2 + "万" + TempStr1;
}
//-------2:转换小数部分
int SNumberLen = SNumber.Length;
string IStr = "";
for(int j = 0;j < SNumberLen;j++)
{
string SymbolStr = j==0?"角":"分";
IStr = ConvertSignCap(int.Parse(SNumber.Substring(j,1))) + SymbolStr;
PartStr2 = PartStr2==""?IStr:(PartStr2 + IStr);
}
//将整数部分和小数部分结合
CapStr = PartStr1 + "元" + PartStr2;
}
}
//-------2:转换小数部分
int SNumberLen = SNumber.Length;
string IStr = "";
for(int j = 0;j < SNumberLen;j++)
{
string SymbolStr = j==0?"角":"分";
IStr = ConvertSignCap(int.Parse(SNumber.Substring(j,1))) + SymbolStr;
PartStr2 = PartStr2==""?IStr:(PartStr2 + IStr);
}
//将整数部分和小数部分结合
CapStr = PartStr1 + "元" + PartStr2;
}
CapStr = CapStr.Replace("拾零","拾");
CapStr = CapStr.Replace("零拾","零");
CapStr = CapStr.Replace("零佰","零");
CapStr = CapStr.Replace("零仟","零");
CapStr = CapStr.Replace("零零","零");
CapStr = CapStr.Replace("零零零","零");
CapStr = CapStr.Replace("零零零零","零");
CapStr = CapStr.Replace("零零","零");
return CapStr;
}
CapStr = CapStr.Replace("零拾","零");
CapStr = CapStr.Replace("零佰","零");
CapStr = CapStr.Replace("零仟","零");
CapStr = CapStr.Replace("零零","零");
CapStr = CapStr.Replace("零零零","零");
CapStr = CapStr.Replace("零零零零","零");
CapStr = CapStr.Replace("零零","零");
return CapStr;
}
说明:只能转换金额数小于1兆,且小数点保留2位,当然也可以做相应修改。
直接调用ConvertToCap方法。
直接调用ConvertToCap方法。
///////////////////////////////////////////////////////////////////////////////
================= 货币金额大写转换库 Money2String.cs ==================
// 货币金额大写转换库 Money2String.dll
// 静态中文转换方法 String GetCnString(String MoneyString)
// 最大转化金额 9999999999999999.99 (16位整数位,2位小数位)
// 方法返回值 转换成功则返回非零长字串,否则返回零长字串
// 静态英文转换方法 String GetEnString(String MoneyString)
// 最大转化金额 999999999999999.99 (15位整数位,2位小数位)
// 方法返回值 转换成功则返回非零长字串,否则返回零长字串
// 程序设计 xlFancy 版本 2003.03.18.22 - 2003.04.09
// 货币金额大写转换库 Money2String.dll
// 静态中文转换方法 String GetCnString(String MoneyString)
// 最大转化金额 9999999999999999.99 (16位整数位,2位小数位)
// 方法返回值 转换成功则返回非零长字串,否则返回零长字串
// 静态英文转换方法 String GetEnString(String MoneyString)
// 最大转化金额 999999999999999.99 (15位整数位,2位小数位)
// 方法返回值 转换成功则返回非零长字串,否则返回零长字串
// 程序设计 xlFancy 版本 2003.03.18.22 - 2003.04.09
using System;
namespace Money2String
{
public class Money2String
{
private static readonly String cnNumber = "零壹贰叁肆伍陆柒捌玖";
private static readonly String cnUnit = "分角元拾佰仟万拾佰仟亿拾佰仟兆拾佰仟";
private static readonly String[] enSmallNumber = {"","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN","ELEVEN","TWELVE","THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTEEN","NINETEEN"};
private static readonly String[] enLargeNumber = {"TWENTY","THIRTY","FORTY","FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY"};
private static readonly String[] enUnit = {"","THOUSAND","MILLION","BILLION","T
namespace Money2String
{
public class Money2String
{
private static readonly String cnNumber = "零壹贰叁肆伍陆柒捌玖";
private static readonly String cnUnit = "分角元拾佰仟万拾佰仟亿拾佰仟兆拾佰仟";
private static readonly String[] enSmallNumber = {"","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN","ELEVEN","TWELVE","THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTEEN","NINETEEN"};
private static readonly String[] enLargeNumber = {"TWENTY","THIRTY","FORTY","FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY"};
private static readonly String[] enUnit = {"","THOUSAND","MILLION","BILLION","T
RILLION"};
// 以下是货币金额中文大写转换方法
public static String GetCnString(String MoneyString)
{
String [] tmpString = MoneyString.Split(\'.\');
String intString = MoneyString; // 默认为整数
String decString = ""; // 保存小数部分字串
String rmbCapital = ""; // 保存中文大写字串
int k;
int j;
int n;
if (tmpString.Length>1)
// 以下是货币金额中文大写转换方法
public static String GetCnString(String MoneyString)
{
String [] tmpString = MoneyString.Split(\'.\');
String intString = MoneyString; // 默认为整数
String decString = ""; // 保存小数部分字串
String rmbCapital = ""; // 保存中文大写字串
int k;
int j;
int n;
if (tmpString.Length>1)
{
intString = tmpString[0]; // 取整数部分
decString = tmpString[1]; // 取小数部分
}
decString += "00";
decString = decString.Substring (0,2); // 保留两位小数位
intString += decString;
try
{
k = intString.Length -1;
if (k>0 && k<18)
{
for (int i=0;i<=k;i++)
{
intString = tmpString[0]; // 取整数部分
decString = tmpString[1]; // 取小数部分
}
decString += "00";
decString = decString.Substring (0,2); // 保留两位小数位
intString += decString;
try
{
k = intString.Length -1;
if (k>0 && k<18)
{
for (int i=0;i<=k;i++)
{
j=(int)intString[i]-48;
// rmbCapital = rmbCapital + cnNumber[j] + cnUnit[k-i]; // 供调试用的直接转换
n = i+1 >= k ? (int)intString[k]-48:(int)intString[i+1]-48; // 等效于 if( ){ }else{ }
if (j==0)
{
if(k-i==2 || k-i==6 || k-i==10 || k-i==14)
{
rmbCapital += cnUnit[k-i];
}
else
{
if (n != 0)
{
rmbCapital += cnNumber[j];
}
// rmbCapital = rmbCapital + cnNumber[j] + cnUnit[k-i]; // 供调试用的直接转换
n = i+1 >= k ? (int)intString[k]-48:(int)intString[i+1]-48; // 等效于 if( ){ }else{ }
if (j==0)
{
if(k-i==2 || k-i==6 || k-i==10 || k-i==14)
{
rmbCapital += cnUnit[k-i];
}
else
{
if (n != 0)
{
rmbCapital += cnNumber[j];
}
}
}
else
{
rmbCapital = rmbCapital + cnNumber[j] + cnUnit[k-i];
}
}
rmbCapital = rmbCapital.Replace ("兆亿万","兆");
rmbCapital = rmbCapital.Replace ("兆亿","兆");
rmbCapital = rmbCapital.Replace ("亿万","亿");
rmbCapital = rmbCapital.TrimStart(\'元\');
rmbCapital = rmbCapital.TrimStart(\'零\');
return rmbCapital;
}
else
{
rmbCapital = rmbCapital + cnNumber[j] + cnUnit[k-i];
}
}
rmbCapital = rmbCapital.Replace ("兆亿万","兆");
rmbCapital = rmbCapital.Replace ("兆亿","兆");
rmbCapital = rmbCapital.Replace ("亿万","亿");
rmbCapital = rmbCapital.TrimStart(\'元\');
rmbCapital = rmbCapital.TrimStart(\'零\');
return rmbCapital;
}
else
{
return ""; // 超出转换范围时,返回零长字串
}
}
catch
{
return ""; // 含有非数值字符时,返回零长字串
}
}
// 以下是货币金额英文大写转换方法
public static String GetEnString(String MoneyString)
else
{
return ""; // 超出转换范围时,返回零长字串
}
}
catch
{
return ""; // 含有非数值字符时,返回零长字串
}
}
// 以下是货币金额英文大写转换方法
public static String GetEnString(String MoneyString)
{
String [] tmpString = MoneyString.Split(\'.\');
String intString = MoneyString; // 默认为整数
String decString = ""; // 保存小数部分字串
String engCapital = ""; // 保存英文大写字串
String strBuff1;
String strBuff2;
String strBuff3;
int curPoint;
int i1;
int i2;
int i3;
int k;
int n;
String [] tmpString = MoneyString.Split(\'.\');
String intString = MoneyString; // 默认为整数
String decString = ""; // 保存小数部分字串
String engCapital = ""; // 保存英文大写字串
String strBuff1;
String strBuff2;
String strBuff3;
int curPoint;
int i1;
int i2;
int i3;
int k;
int n;
if (tmpString.Length >1)
{
intString = tmpString[0]; // 取整数部分
decString = tmpString[1]; // 取小数部分
}
decString += "00";
decString = decString.Substring (0,2); // 保留两位小数位
try
{
// 以下处理整数部分
curPoint = intString.Length-1;
if (curPoint>-1 && curPoint<15)
{
k = 0;
{
intString = tmpString[0]; // 取整数部分
decString = tmpString[1]; // 取小数部分
}
decString += "00";
decString = decString.Substring (0,2); // 保留两位小数位
try
{
// 以下处理整数部分
curPoint = intString.Length-1;
if (curPoint>-1 && curPoint<15)
{
k = 0;
while(curPoint>=0)
{
strBuff1 = "";
strBuff2 = "";
strBuff3 = "";
if (curPoint>=2)
{
n=int.Parse(intString.Substring(curPoint-2,3));
if (n !=0)
{
i1 = n/100; // 取佰位数值
i2 = (n-i1*100)/10; // 取拾位数值
i3 = n-i1*100-i2*10; // 取个位数值
if (i1 !=0)
{
{
strBuff1 = "";
strBuff2 = "";
strBuff3 = "";
if (curPoint>=2)
{
n=int.Parse(intString.Substring(curPoint-2,3));
if (n !=0)
{
i1 = n/100; // 取佰位数值
i2 = (n-i1*100)/10; // 取拾位数值
i3 = n-i1*100-i2*10; // 取个位数值
if (i1 !=0)
{
strBuff1 = enSmallNumber[i1] + " HUNDRED ";
}
if (i2!=0)
{
if (i2==1)
{
strBuff2 = enSmallNumber[i2*10+i3] + " ";
}
else
{
strBuff2 = enLargeNumber[i2-2] + " ";
if (i3 !=0)
{
strBuff3 = enSmallNumber[i3] + " ";
}
}
if (i2!=0)
{
if (i2==1)
{
strBuff2 = enSmallNumber[i2*10+i3] + " ";
}
else
{
strBuff2 = enLargeNumber[i2-2] + " ";
if (i3 !=0)
{
strBuff3 = enSmallNumber[i3] + " ";
}
}
}
else
{
if (i3 !=0)
{
strBuff3 = enSmallNumber[i3] + " ";
}
}
engCapital = strBuff1 + strBuff2 + strBuff3 + enUnit[k] + " " + engCapital;
}
}
else
{
n = int.Parse(intString.Substring(0,curPoint+1));
}
else
{
if (i3 !=0)
{
strBuff3 = enSmallNumber[i3] + " ";
}
}
engCapital = strBuff1 + strBuff2 + strBuff3 + enUnit[k] + " " + engCapital;
}
}
else
{
n = int.Parse(intString.Substring(0,curPoint+1));
if (n !=0)
{
i2 = n/10; // 取拾位数值
字符串截取后四位方法 i3 = n-i2*10; // 取个位数值
if (i2!=0)
{
if (i2==1)
{
strBuff2 = enSmallNumber[i2*10+i3] + " ";
}
else
{
strBuff2 = enLargeNumber[i2-2] + " ";
if (i3 !=0)
{
{
i2 = n/10; // 取拾位数值
字符串截取后四位方法 i3 = n-i2*10; // 取个位数值
if (i2!=0)
{
if (i2==1)
{
strBuff2 = enSmallNumber[i2*10+i3] + " ";
}
else
{
strBuff2 = enLargeNumber[i2-2] + " ";
if (i3 !=0)
{
strBuff3 = enSmallNumber[i3] + " ";
}
}
}
else
{
if (i3 !=0)
{
strBuff3 = enSmallNumber[i3] + " ";
}
}
engCapital = strBuff2 + strBuff3 + enUnit[k] + " " + engCapital;
}
}
}
}
}
else
{
if (i3 !=0)
{
strBuff3 = enSmallNumber[i3] + " ";
}
}
engCapital = strBuff2 + strBuff3 + enUnit[k] + " " + engCapital;
}
}
++k;
curPoint -= 3;
}
engCapital = engCapital.TrimEnd();
}
// 以下处理小数部分
strBuff2 = "";
strBuff3 = "";
n = int.Parse(decString);
if (n !=0)
{
i2 = n/10; // 取拾位数值
i3 = n-i2*10; // 取个位数值
curPoint -= 3;
}
engCapital = engCapital.TrimEnd();
}
// 以下处理小数部分
strBuff2 = "";
strBuff3 = "";
n = int.Parse(decString);
if (n !=0)
{
i2 = n/10; // 取拾位数值
i3 = n-i2*10; // 取个位数值
if (i2!=0)
{
if (i2==1)
{
strBuff2 = enSmallNumber[i2*10+i3] + " ";
}
else
{
strBuff2 = enLargeNumber[i2-2] + " ";
if (i3 !=0)
{
strBuff3 = enSmallNumber[i3] + " ";
}
}
}
{
if (i2==1)
{
strBuff2 = enSmallNumber[i2*10+i3] + " ";
}
else
{
strBuff2 = enLargeNumber[i2-2] + " ";
if (i3 !=0)
{
strBuff3 = enSmallNumber[i3] + " ";
}
}
}
else
{
if (i3 !=0)
{
strBuff3 = enSmallNumber[i3] + " ";
}
}
// 将小数字串追加到整数字串后
if (engCapital.Length>0)
{
engCapital = engCapital + " AND CENTS " + strBuff2+strBuff3; // 有整数部分时
}
else
{
{
if (i3 !=0)
{
strBuff3 = enSmallNumber[i3] + " ";
}
}
// 将小数字串追加到整数字串后
if (engCapital.Length>0)
{
engCapital = engCapital + " AND CENTS " + strBuff2+strBuff3; // 有整数部分时
}
else
{
engCapital = "CENTS " + strBuff2 + strBuff3; // 只有小数部分时
}
}
engCapital = engCapital.TrimEnd();
return engCapital;
}
catch
{
return ""; // 含非数字字符时,返回零长字串
}
}
}
}
}
}
engCapital = engCapital.TrimEnd();
return engCapital;
}
catch
{
return ""; // 含非数字字符时,返回零长字串
}
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论