本人现在做的项目要求对数值型货币,转换成大写,在网上搜索了半天,没有找到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;
}
}
}
本文地址:http://com.8s8s.com/it/it42993.htm