/// <summary>
    /// 转为大写人民币
    /// </summary>
    /// <param name="x"></param>
    /// <returns></returns>
    public static string ToRMB(string x)
    {
        string ret = "";
        int nnum;
        x = x.Replace("-", "");
        if (x.IndexOf(".") > -1)
        {
            if (x.Length == (x.IndexOf(".") + 2)) x = x + "0";
            nnum = int.Parse(x.Substring(0, x.IndexOf(".")));
            if (x.Substring(x.IndexOf(".") + 1, 2) == "00")
                ret = ToInt(nnum.ToString()) + "元整";
            else ret = ToInt(nnum.ToString()) + "元" + ToDot(x.Substring(x.IndexOf(".") + 1, 2));
        }
        else
        {
            ret = ToInt(x) + "元整";
        }
        return ret;
    }
    private static char ToNum(char x)
    {
        string strChnNames = "零壹贰叁肆伍陆柒捌玖";
        string strNumNames = "0123456789";
        return strChnNames[strNumNames.IndexOf(x)];
    }
    private static string ChangeInt(string x)
    {
        string[] strArrayLevelNames = new string[4] { "", "拾", "佰", "仟" };
        string ret = "";
        int i;
        for (i = x.Length - 1; i >= 0; i--)
            if (x[i] == '0')
                ret = ToNum(x[i]) + ret;
            else
                ret = ToNum(x[i]) + strArrayLevelNames[x.Length - 1 - i] + ret;
        while ((i = ret.IndexOf("零零")) != -1)
            ret = ret.Remove(i, 1);
        if (ret[ret.Length - 1] == '零' && ret.Length > 1)
            ret = ret.Remove(ret.Length - 1, 1);
        if (ret.Length >= 2 && ret.Substring(0, 2) == "壹拾")
            ret = ret.Remove(0, 1);
        return ret;
    }
    private static string ToInt(string x)
    {
        int len = x.Length;
        string ret, temp;
        if (len <= 4)
            ret = ChangeInt(x);
        else if (len <= 8)
        {
            ret = ChangeInt(x.Substring(0, len - 4)) + "万";
            temp = ChangeInt(x.Substring(len - 4, 4));
            if (temp.IndexOf("仟") == -1 && temp != "")
                ret += "零" + temp;
            else
                ret += temp;
        }
        else
        {
            ret = ChangeInt(x.Substring(0, len - 8)) + "亿";
            temp = ChangeInt(x.Substring(len - 8, 4));
            if (temp.IndexOf("仟") == -1 && temp != "")
                ret += "零" + temp;
            else
                ret += temp;
            ret += "万";
            temp = ChangeInt(x.Substring(len - 4, 4));
            if (temp.IndexOf("仟") == -1 && temp != "")
                ret += "零" + temp;
            else
                ret += temp;
        }
        int i;
        if ((i = ret.IndexOf("零万")) != -1)
            ret = ret.Remove(i + 1, 1);
        while ((i = ret.IndexOf("零零")) != -1)
            ret = ret.Remove(i, 1);
        if (ret[ret.Length - 1] == '零' && ret.Length > 1)
            ret = ret.Remove(ret.Length - 1, 1);
        return ret;
    }
    private static string ToDot(string x)
    {
        string ret = "";
        string sdot = "角分";
        if (x.Length > 2) x = x.Substring(0, 2);
        for (int i = 0; i < x.Length; i++)
        {
            ret += ToNum(x[i]) + sdot.Substring(i, 1);
        }
        if (ret.IndexOf("零角") > -1) ret = ret.Substring(2, 2);
        if (ret.IndexOf("零分") > -1) ret = ret.Substring(0, 2);
        return ret;
    }
posted on 2007-01-30 10:50  微星  阅读(643)  评论(0编辑  收藏  举报