人民币金额转大写

2014-06-18 11:56:33  访问(2294) 赞(0) 踩(0)

  • public class MoneyFormat
        {
            public static string GetMoneyStr(float num)//总
            {
                try
                {
                    string m_point = "圆";
                    string m_sign;
                    int m_len;
                    //int g_max=20;
                    int g_dec = 2;
                    string m_srcint;
                    string m_srcdec;
                    string m_retv = "";
                    int m_cntr = 0;
                    string m_decstr;
    
                    if (num >= 0.00)
                    {
                        m_sign = "";
                    }
                    else
                    {
                        m_sign = "负";
                        num = num * (-1);
                    }
                    m_srcint = num.ToString().Trim();//m_srcint--"5658626456.235"
                    string[] s_cut = m_srcint.Split(new char[] { '.' });
                    if (s_cut.Length == 2)
                    {
                        if (s_cut[0].ToString().Trim().Length > 15)
                        {
                            m_retv = m_sign + m_retv + m_srcint + "转换失败!";
                            return m_retv;
    
                        }
                        if (s_cut[1].ToString().Trim().Length > 4)
                        {
                            m_srcint = m_srcint.Substring(0, m_srcint.Length - (s_cut[1].ToString().Trim().Length - 4));
                        }
                    }
                    else if (s_cut.Length == 1)
                    {
                        m_srcint = m_srcint + ".0000";
                    }
                    else
                    {
                        return num.ToString() + "转换失败!";
                    }
                    s_cut = m_srcint.Split(new char[] { '.' }, 2);
    
                    m_srcdec = s_cut[1].ToString().Trim();
                    m_srcdec = m_srcdec + "0000";
                    m_srcdec = m_srcdec.Substring(0, 4);
    
                    m_srcint = s_cut[0].ToString().Trim();
                    m_len = m_srcint.Length;
    
                    string m_chr, m_last = "", m_this = "", m_cnzero = Num2cn("0"), m_cnname, m_lbase, m_tbase = Len2cnbase(m_len);
                    m_lbase = m_tbase;
                    for (m_cntr = 1; m_cntr <= m_len; m_cntr++)
                    {
                        m_chr = m_srcint.Substring(m_cntr - 1, 1);
                        m_this = Num2cn(m_chr);
                        m_cnname = Len2cnname(m_len - m_cntr + 1);
                        m_tbase = Len2cnbase(m_len - m_cntr + 1);
                        if (m_lbase == m_tbase)
                        {
                            if (m_last == m_cnzero && m_this == m_cnzero)
                            { }
                            else
                            {
                                if ((m_this == m_cnzero))
                                    m_retv = m_retv + m_this + "";
                                else
                                    m_retv = m_retv + m_this + m_cnname;
                            }
                        }
                        else
                        {
                            if (m_retv.Length >= 2)
                            {
                                if (m_retv.Substring(m_retv.Length - 2, 2) == m_cnzero)
                                {
                                    m_retv = m_retv.Substring(0, m_retv.Length - 2);
                                    m_last = "";
                                }
                            }
                            m_retv = m_retv + m_lbase;
                            if (m_last == m_cnzero && m_this == m_cnzero)
                            { }
                            else
                            {
                                if ((m_this == m_cnzero))
                                    m_retv = m_retv + m_this + "";
                                else
                                    m_retv = m_retv + m_this + m_cnname;
                            }
                        }
                        m_lbase = m_tbase;
                        m_last = m_this;
                    }
                    if (m_retv.EndsWith(m_cnzero))
                        m_retv = m_retv.Substring(0, m_retv.Length - m_cnzero.Length);
                    if (m_retv.Length - 2 >= 0)
                    {
                        if (m_retv.Substring(m_retv.Length - 2, 2) == m_cnzero)
                        {
                            m_retv = m_retv.Substring(0, m_retv.Length - 2);
                        }
                    }
                    m_retv = m_retv + m_point;
                    m_decstr = "";
    
                    if (double.Parse(m_srcdec) == 0)
                    {
                        if (m_point == "圆")
                            m_retv = m_retv + "整";
                        else
                            m_retv = m_retv + "";
                    }
                    else
                    {
                        m_len = g_dec;
                        m_this = "";
                        m_last = "";
                        for (m_cntr = m_len; m_cntr > 0; m_cntr--)
                        {
                            m_chr = m_srcdec.Substring(m_cntr - 1, 1);
                            m_this = Num2cn(m_chr);
                            if (m_point == "圆")
                            {
    
                                m_cnname = Jedec(m_cntr);
                                if (m_this == m_cnzero && null == m_decstr)
                                { }
                                else
                                {
                                    if (m_last == m_cnzero && m_this == m_cnzero)
                                    { }
                                    else
                                    {
                                        if (m_this == m_cnzero)
                                            m_decstr = m_this + "" + m_decstr;
                                        else
                                            m_decstr = m_this + m_cnname + m_decstr;
                                    }
                                }
                            }
                            else
                            {
                                m_cnname = "";
                                if (m_this == m_cnzero && null == m_decstr)
                                { }
                                else
                                    m_decstr = m_this + m_decstr;
    
                            }
                            m_last = m_this;
                        }
                    }
                    string myretu = m_sign + m_retv + m_decstr;
                    if (myretu.Substring(myretu.Length - 1, 1) == "零")
                        return myretu.Substring(0, myretu.Length - 1);
                    else
                        return myretu;
                }
                catch
                {
                    return num.ToString() + "转换失败!";
                }
    
            }
            #region 1--"角" 2--"分"
            private static string Jedec(int num)
            {
                string retu = "";
                switch (num)
                {
                    case 1:
                        retu = "角";
                        break;
                    case 2:
                        retu = "分";
                        break;
                    case 3:
                        retu = "厘";
                        break;
                    case 4:
                        retu = "毫";
                        break;
                    default:
                        retu = "";
                        break;
                }
                return retu;
            }
    
            #endregion
            #region cn >=14 && cn<=19--"兆"
            private static string Len2cnbase(int cn)
            {
                string retu = "";
                if (cn >= 14 && cn <= 19)
                {
                    retu = "兆";
                }
                else if (cn >= 9 && cn <= 13)
                {
                    retu = "亿";
                }
                else if (cn >= 5 && cn <= 8)
                {
                    retu = "万";
                }
                else if (cn < 5)
                {
                    retu = "";
                }
                else
                {
                    retu = "N/A";
                }
                return retu;
            }
            #endregion
            #region 1--"",2--"拾"
            private static string Len2cnname(int len)
            {
                string retu = "";
                switch (len)
                {
                    case 1:
                        retu = "";
                        break;
                    case 2:
                        retu = "拾";
                        break;
                    case 3:
                        retu = "佰";
                        break;
                    case 4:
                        retu = "仟";
                        break;
                    case 5:
                        retu = "";
                        break;
                    case 6:
                        retu = "拾";
                        break;
                    case 7:
                        retu = "佰";
                        break;
                    case 8:
                        retu = "仟";
                        break;
                    case 9:
                        retu = "";
                        break;
                    case 10:
                        retu = "拾";
                        break;
                    case 11:
                        retu = "佰";
                        break;
                    case 12:
                        retu = "仟";
                        break;
                    case 13:
                        retu = "万";
                        break;
                    case 14:
                        retu = "";
                        break;
                    case 15:
                        retu = "拾";
                        break;
                    case 16:
                        retu = "佰";
                        break;
                    default:
                        retu = "N/A";
                        break;
                }
                return retu;
            }
            #endregion
            #region "0"--"零" 1--"壹"
            private static string Num2cn(string numchr)
            {
                string retu = "";
                switch (numchr)
                {
                    case "0":
                        retu = "零";
                        break;
                    case "1":
                        retu = "壹";
                        break;
                    case "2":
                        retu = "贰";
                        break;
                    case "3":
                        retu = "叁";
                        break;
                    case "4":
                        retu = "肆";
                        break;
                    case "5":
                        retu = "伍";
                        break;
                    case "6":
                        retu = "陆";
                        break;
                    case "7":
                        retu = "柒";
                        break;
                    case "8":
                        retu = "捌";
                        break;
                    case "9":
                        retu = "玖";
                        break;
                    default:
                        retu = numchr;
                        break;
                }
                return retu;
            }
            #endregion
        }
    

    调用方法

    GetMoneyStr(111134.43f)
    


上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)