將人民幣的數字表示轉化成大寫表示(C#版)
2024-07-21 02:19:26
供稿:網友
將人民幣的數字表示轉化成大寫表示(c#版)
實在沒有什么好講的,就是一個方法,大家拿去用吧
using system;
namespace test.com
{
/// <summary>
/// 功能:字符串處理函數集
/// </summary>
public class dealstring
{
#region 私有成員
/// <summary>
/// 輸入字符串
/// </summary>
private string inputstring=null;
/// <summary>
/// 輸出字符串
/// </summary>
private string outstring=null;
/// <summary>
/// 提示信息
/// </summary>
private string notemessage=null;
#endregion
#region 公共屬性
/// <summary>
/// 輸入字符串
/// </summary>
public string inputstring
{
get{return inputstring;}
set{inputstring=value;}
}
/// <summary>
/// 輸出字符串
/// </summary>
public string outstring
{
get{return outstring;}
set{outstring=value;}
}
/// <summary>
/// 提示信息
/// </summary>
public string notemessage
{
get{return notemessage;}
set{notemessage=value;}
}
#endregion
#region 構造函數
public dealstring()
{
//
// todo: 在此處添加構造函數邏輯
//
}
#endregion
#region 公共方法
public void converttochinesenum()
{
string numlist="零壹貳叁肆伍陸柒捌玖";
string rmblist = "分角元拾佰仟萬拾佰仟億拾佰仟萬";
double number=0;
string tempoutstring=null;
try
{
number=double.parse(this.inputstring);
}
catch
{
this.notemessage="傳入參數非數字!";
return;
}
if(number>9999999999999.99)
this.notemessage="超出范圍的人民幣值";
//將小數轉化為整數字符串
string tempnumberstring=convert.toint64(number*100).tostring();
int tempnmberlength=tempnumberstring.length;
int i=0;
while(i<tempnmberlength)
{
int onenumber=int32.parse(tempnumberstring.substring(i,1));
string onenumberchar=numlist.substring(onenumber,1);
string onenumberunit=rmblist.substring(tempnmberlength-i-1,1);
if(onenumberchar!="零")
tempoutstring+=onenumberchar+onenumberunit;
else
{
if(onenumberunit=="億"||onenumberunit=="萬"||onenumberunit=="元"||onenumberunit=="零")
{
while (tempoutstring.endswith("零"))
{
tempoutstring=tempoutstring.substring(0,tempoutstring.length-1);
}
}
if(onenumberunit=="億"||(onenumberunit=="萬"&&!tempoutstring.endswith("億"))||onenumberunit=="元")
{
tempoutstring+=onenumberunit;
}
else
{
bool tempend=tempoutstring.endswith("億");
bool zeroend=tempoutstring.endswith("零");
if(tempoutstring.length>1)
{
bool zerostart=tempoutstring.substring(tempoutstring.length-2,2).startswith("零");
if(!zeroend&&(zerostart||!tempend))
tempoutstring+=onenumberchar;
}
else
{
if(!zeroend&&!tempend)
tempoutstring+=onenumberchar;
}
}
}
i+=1;
}
while (tempoutstring.endswith("零"))
{
tempoutstring=tempoutstring.substring(0,tempoutstring.length-1);
}
while(tempoutstring.endswith("元"))
{
tempoutstring=tempoutstring+"整";
}
this.outstring=tempoutstring;
}
#endregion
}
}