本人現在做的項目要求對數值型貨幣,轉換成大寫,在網上搜索了半天,沒有找到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;
}
}
}