(C#)數值型貨幣的大寫轉換
2024-07-21 02:18:39
供稿:網友
本人現在做的項目要求對數值型貨幣,轉換成大寫,在網上搜索了半天,沒有找到c#寫的類型,不得不自己寫了,測試了,還能滿足要求,只是算法有點繁瑣,有哪位大蝦再給改改!
下面就是我得代碼:
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
namespace winmarket
{
/// <summary>
/// classfun 的摘要說明。
/// </summary>
public class classfun
{
private string capstr;
public classfun()
{
//
// todo: 在此處添加構造函數邏輯
//
}
public string moneynumtocap(decimal num)
{
string cap="零壹貳叁肆伍陸柒捌玖";
string numstr="0123456789";
string moneynumstr=num.tostring();
int pint=moneynumstr.indexof(".");
int numint;
string moneyint=null;
string moneydec=null;
string intstr=null;
string moneycap=null;
string moneyintstr=null;
string moneydecstr=null;
// capstr=pint.tostring();
if(pint!=-1)
{
string strarr=".";
char[] chararr=strarr.tochararray();
string[] moneynumarr=moneynumstr.split(chararr);
moneyint=moneynumarr[0].tostring();
moneydec=moneynumarr[1].tostring();
}
else
{
moneyint=moneynumstr;
moneydec="00";
}
if(moneyint.length>16)
{
messagebox.show("數值超界");
}
else
{
//--- 處理整數部分--------
for(int j=1;j<=moneyint.length;j++)
{
moneyintstr=moneyint.substring(j-1,1);
for(int i=0;i<=9;i++)
{
intstr=numstr.substring(i,1);
moneycap=cap.substring(i,1);
if(moneyintstr==intstr)
{
switch (intstr)
{
case "0":
capstr=capstr+moneycap;
break;
case "1":
capstr=capstr+moneycap;
break;
case "2":
capstr=capstr+moneycap;
break;
case "3":
capstr=capstr+moneycap;
break;
case "4":
capstr=capstr+moneycap;
break;
case "5":
capstr=capstr+moneycap;
break;
case "6":
capstr=capstr+moneycap;
break;
case "7":
capstr=capstr+moneycap;
break;
case "8":
capstr=capstr+moneycap;
break;
case "9":
capstr=capstr+moneycap;
break;
}
}
}
numint=moneyint.length-j+1;
switch (numint)
{
case 16:
capstr=capstr+"仟萬";
break;
case 15:
capstr=capstr+"佰萬";
break;
case 14:
capstr=capstr+"拾萬";
break;
case 13:
capstr=capstr+"萬";
break;
case 12:
capstr=capstr+"仟";
break;
case 11:
capstr=capstr+"佰";
break;
case 10:
capstr=capstr+"拾";
break;
case 9:
capstr=capstr+"億";
break;
case 8:
capstr=capstr+"仟";
break;
case 7:
capstr=capstr+"佰";
break;
case 6:
capstr=capstr+"拾";
break;
case 5:
capstr=capstr+"萬";
break;
case 4:
capstr=capstr+"仟";
break;
case 3:
capstr=capstr+"佰";
break;
case 2:
capstr=capstr+"拾";
break;
case 1:
capstr=capstr+"元";
break;
}
}
//------處理小數部分-----
for(int j=1; j<=2; j++)
{
moneydecstr=moneydec.substring(j-1,1);
for(int i=0;i<=9;i++)
{
intstr=numstr.substring(i,1);
moneycap=cap.substring(i,1);
if(moneydecstr==intstr)
{
switch (intstr)
{
case "0":
capstr=capstr+moneycap;
break;
case "1":
capstr=capstr+moneycap;
break;
case "2":
capstr=capstr+moneycap;
break;
case "3":
capstr=capstr+moneycap;
break;
case "4":
capstr=capstr+moneycap;
break;
case "5":
capstr=capstr+moneycap;
break;
case "6":
capstr=capstr+moneycap;
break;
case "7":
capstr=capstr+moneycap;
break;
case "8":
capstr=capstr+moneycap;
break;
case "9":
capstr=capstr+moneycap;
break;
}
}
}
switch(j)
{
case 1:
capstr=capstr+"角";
break;
case 2:
capstr=capstr+"分";
break;
}
}
}
return capstr;
}
}
}