C#中金额大小写的转换
请各位同仁帮忙,帮小第写个金额大小写的转换的方法,用C#

解决方案 »

  1.   

    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;      //找出最高位 
    if (j > 15){return "溢出";} 
    str2 = str2.Substring(15-j);   //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分 
                   
    //循环取出每一位需要转换的值 
    for(i=0;i<j;i++) 

    str3 = str4.Substring(i,1);          //取出需转换的某一位的值 
    temp = Convert.ToInt32(str3);      //转换为数字 
    if (i != (j-3) && i != (j-7) && i != (j-11) && i != (j-15)) 
    {     
    //当所取位数不为元、万、亿、万亿上的数字时 
    if (str3 == "0") 

    ch1 = ""; 
    ch2 = ""; 
    nzero = nzero + 1; 

    else 

    if(str3 != "0" && nzero != 0) 

    ch1 = "零" + str1.Substring(temp*1,1); 
    ch2 = str2.Substring(i,1); 
    nzero = 0; 

    else 

    ch1 = str1.Substring(temp*1,1); 
    ch2 = str2.Substring(i,1); 
    nzero = 0; 



    else 
    {  
    //该位是万亿,亿,万,元位等关键位 
    if (str3 != "0" && nzero != 0) 

    ch1 = "零" + str1.Substring(temp*1,1); 
    ch2 = str2.Substring(i,1); 
    nzero = 0; 

    else 

    if (str3 != "0" && nzero == 0) 

    ch1 = str1.Substring(temp*1,1); 
    ch2 = str2.Substring(i,1); 
    nzero = 0; 

    else 

    if (str3 == "0" && nzero >= 3) 

    ch1 = ""; 
    ch2 = ""; 
    nzero = nzero + 1; 

    else 

    if (j >= 11) 

    ch1 = ""; 
    nzero = nzero + 1; 

    else 

    ch1 = ""; 
    ch2 = str2.Substring(i,1); 
    nzero = nzero + 1; 





    if (i == (j-11) || i == (j-3)) 
    {  
    //如果该位是亿位或元位,则必须写上 
    ch2 = str2.Substring(i,1); 

    str5 = str5 + ch1 + ch2; 
         
    if (i == j-1 && str3 == "0" ) 
    {    
    //最后一位(分)为0时,加上“整” 
    str5 = str5 + '整'; 


    if (m_theMoney == 0) 

    str5 = "零元整"; 

    return str5; 
    }  /// <summary>
    /// 根据钱数变换字体颜色
    /// </summary>
    /// <param name="theMoney">钱数</param>
    /// <param name="dotLong">小数后几位</param>
    /// <param name="moneyType">InCome或者Expenses</param>
    /// <returns></returns>
    public string IncomeAndExpensesString(int dotLong,string moneyType)
    {
    string returnString = String.Empty;
    double returnMoney = Math.Abs(this.m_thedMoney);
    RegionInfo rTemp = new RegionInfo("CN");
    switch(dotLong)
    {
    case 2:
    string theColor = this.GetStringColor(moneyType);
    returnString = "<font color='"+theColor+"'>"+returnMoney.ToString(rTemp.CurrencySymbol+"0.00")+"</font>";
    break;
    default:
    returnString = this.m_thedMoney.ToString();
    break;
    }
    return returnString;
    } private string GetStringColor(string moneyType)
    {
    string returnString = String.Empty;
    if(this.m_thedMoney<0)
    {
    returnString = "red";
    }
    else if(this.m_thedMoney==0)
    {
    returnString = "black";
    }
    else if(moneyType=="InCome")
    {
    returnString = "green";
    }
    else if(moneyType == "Expenses")
    {
    returnString = "red";
    }
    else if(moneyType == String.Empty || moneyType == "Default")
    {
    if(this.m_thedMoney>0)
    {
    returnString = "green";
    }
    else if(this.m_thedMoney==0)
    {
    returnString = "black";
    }
    else
    {
    returnString = "red";
    }
    }
    return returnString;
    }
    }
      

  2.   

    看这篇文章
    http://www.cnblogs.com/esshs/archive/2005/03/30/128318.aspx
      

  3.   

    http://singlepine.cnblogs.com/articles/262701.html
      

  4.   

    sql 版:CREATE FUNCTION [dbo].[f_num_chn] (@num numeric(14,2))
    RETURNS varchar(100) WITH ENCRYPTION
    AS
    BEGIN
    --版权所有:pbsql
      DECLARE @n_data VARCHAR(20),@c_data VARCHAR(100),@n_str VARCHAR(10),@i int  SET @n_data=RIGHT(SPACE(14)+CAST(CAST(ABS(@num*100) AS bigint) AS varchar(20)),14)
      SET @c_data=''
      SET @i=1
      WHILE @i<=14
      BEGIN
        SET @n_str=SUBSTRING(@n_data,@i,1)
        IF @n_str<>' '
        BEGIN
          IF not ((SUBSTRING(@n_data,@i,2)='00') or
            ((@n_str='0') and ((@i=4) or (@i=8) or (@i=12) or (@i=14))))
            SET @c_data=@c_data+SUBSTRING('零壹贰叁肆伍陆柒捌玖',CAST(@n_str AS int)+1,1)
          IF not ((@n_str='0') and (@i<>4) and (@i<>8) and (@i<>12))
            SET @c_data=@c_data+SUBSTRING('仟佰拾亿仟佰拾万仟佰拾圆角分',@i,1)
          IF SUBSTRING(@c_data,LEN(@c_data)-1,2)='亿万'
            SET @c_data=SUBSTRING(@c_data,1,LEN(@c_data)-1)
        END
        SET @i=@i+1
      END
      IF @num<0
        SET @c_data='(负数)'+@c_data
      IF @num=0
        SET @c_data='零圆'
      IF @n_str='0'
        SET @c_data=@c_data+'整'
      RETURN(@c_data)
    END
      

  5.   

    http://blog.csdn.net/abandonship/archive/2005/11/21/533881.aspx这是我以前写过的一个(存储过程版)英文版大小写,你转一下就可以用了