当前位置: 代码迷 >> ASP.NET >> C# 小写金额替换成大写金额,该如何处理
  详细解决方案

C# 小写金额替换成大写金额,该如何处理

热度:4653   发布时间:2013-02-25 00:00:00.0
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; //找出最高位
  相关解决方案