数字转换成中文大小写、金额大小写

  将数字转换成中文大小写、金额大小写 

        /// <summary>
        /// 将数字转换成中文大写
        /// </summary>
        /// <param name="Value">数字</param>
        /// <param name="bToUpper">是否转换成汉字大写 true表示大写 false表示小写</param>
        /// <param name="bMoney">是否转换成金额格式 true表示金额格式  false表示普通格式</param>
        /// <returns></returns>
        public static string DataConvert(double Value, bool bToUpper = true, bool bMoney = true)
        {
            if (Value == 0 || Value > 999999999999.99)
            {
                if (Value == 0 && !bMoney)
                {
                    return "";
                }
                else
                {
                    return "";
                }
            }
            string str = Value.ToString("F2");
            string sLast = "";//小数点后面的数据
            string fuhao = "";//负号
            string[] Cnum = new string[10];
            string[] Cbit = new string[12];
            Cnum[0] = "";
            Cnum[1] = (bToUpper ? "" : "");
            Cnum[2] = (bToUpper ? "" : "");
            Cnum[3] = (bToUpper ? "" : "");
            Cnum[4] = (bToUpper ? "" : "");
            Cnum[5] = (bToUpper ? "" : "");
            Cnum[6] = (bToUpper ? "" : "");
            Cnum[7] = (bToUpper ? "" : "");
            Cnum[8] = (bToUpper ? "" : "");
            Cnum[9] = (bToUpper ? "" : "");
            Cbit[0] = "";
            Cbit[1] = (bToUpper ? "" : "");
            Cbit[2] = (bToUpper ? "" : "");
            Cbit[3] = (bToUpper ? "" : "");
            Cbit[4] = "";
            Cbit[5] = (bToUpper ? "" : "");
            Cbit[6] = (bToUpper ? "" : "");
            Cbit[7] = (bToUpper ? "" : "");
            Cbit[8] = "亿";
            Cbit[9] = (bToUpper ? "" : "");
            Cbit[10] = (bToUpper ? "" : "");
            Cbit[11] = (bToUpper ? "" : "");
            if (!bMoney)
            {
                string[] arrayValue = Value.ToString().Split('.');
                sLast = arrayValue.Length > 1 ? arrayValue[1] : "";
            }
            if (Value < 0)
            {
                str = str.ToString().Remove(0, 1);//去掉负号
                fuhao = "";
            }
            int MaxLen = str.Length;
            bool zoo = true;
            bool Needzoo = false;
            bool HaveWan = false;
            bool HaveYi = false;
            int tmpVal = 0;
            string[] strBit = new string[MaxLen];
            string ConvertMoney = "";
            int i = 0;
            for (i = 0; i < MaxLen; i++)
            {
                if (i != 2)
                {
                    tmpVal = Convert.ToInt32(str.Substring(MaxLen - i - 1, 1));
                }
                switch (i)
                {
                    case 0:
                        if (bMoney)
                        {
                            if (tmpVal != 0)
                            {
                                strBit[i] = Cnum[tmpVal] + "";
                            }
                        }
                        break;
                    case 1:
                        if (bMoney)
                        {
                            if (tmpVal != 0)
                            {
                                strBit[i] = Cnum[tmpVal] + "";
                            }
                        }
                        break;
                    case 2:

                        break;
                    case 3:
                        if (tmpVal != 0)
                        {
                            strBit[i] = Cnum[tmpVal] + (bMoney ? (bToUpper ? "" : "") : "");
                            zoo = false;
                        }
                        else
                        {
                            Needzoo = true;
                            if (MaxLen > 4)
                            {
                                strBit[3] = (bMoney ? (bToUpper ? "" : "") : "");
                            }
                        }
                        if (strBit[1] == "" && strBit[0] != "" && strBit[3] != "")
                        {
                            strBit[i] = strBit[i] + "";
                        }
                        break;
                    case 4:
                        if (tmpVal == 0)
                        {
                            Needzoo = true;
                        }
                        else
                        {
                            strBit[i] = Cnum[tmpVal] + (bToUpper ? "" : "");
                            if (Needzoo)
                            {
                                if (zoo)
                                {
                                    strBit[i] = (strBit[3] == "" ? strBit[i] + (bMoney ? (bToUpper ? "" : "") : "") : strBit[i]);
                                }
                                else
                                {
                                    strBit[i] = strBit[i] + "";
                                }
                            }
                            zoo = false;
                            Needzoo = false;
                        }
                        break;
                    case 5:
                        if (tmpVal == 0)
                        {
                            Needzoo = true;
                        }
                        else
                        {
                            strBit[i] = Cnum[tmpVal] + (bToUpper ? "" : "");
                            if (Needzoo)
                            {
                                if (zoo)
                                {
                                    strBit[i] = (strBit[3] == "" ? strBit[i] + (bMoney ? (bToUpper ? "" : "") : "") : strBit[i]);
                                }
                                else
                                {
                                    strBit[i] = strBit[i] + "";
                                }
                            }
                            zoo = false;
                            Needzoo = false;
                        }
                        break;
                    case 6:
                        if (tmpVal == 0)
                        {
                            Needzoo = true;
                        }
                        else
                        {
                            strBit[i] = Cnum[tmpVal] + (bToUpper ? "" : "");
                            if (Needzoo)
                            {
                                if (zoo)
                                {
                                    strBit[i] = (strBit[3] == "" ? strBit[i] + (bMoney ? (bToUpper ? "" : "") : "") : strBit[i]);
                                }
                                else
                                {
                                    strBit[i] = strBit[i] + "";
                                }
                            }
                            zoo = false;
                            Needzoo = false;
                        }
                        break;
                    case 7:
                        if (tmpVal == 0)
                        {
                            Needzoo = true;
                        }
                        else
                        {
                            strBit[i] = Cnum[tmpVal] + "";
                            if (Needzoo)
                            {
                                if (zoo)
                                {
                                    strBit[i] = (strBit[3] == "" ? strBit[i] + (bMoney ? (bToUpper ? "" : "") : "") : strBit[i]);
                                }
                                else
                                {
                                    strBit[i] = strBit[i] + "";
                                }
                            }
                            zoo = false;
                            Needzoo = false;
                            HaveWan = true;
                        }
                        break;
                    case 8:
                        if (tmpVal == 0)
                        {
                            Needzoo = true;
                        }
                        else
                        {
                            strBit[i] = Cnum[tmpVal] + (bToUpper ? "" : "");
                            if (!HaveWan)
                            {
                                strBit[i] = strBit[i] + "";
                            }
                            HaveWan = true;
                            if (Needzoo)
                            {
                                if (zoo)
                                {
                                    strBit[i] = (strBit[3] == "" ? strBit[i] + (bMoney ? (bToUpper ? "" : "") : "") : strBit[i]);
                                }
                                else
                                {
                                    strBit[i] = strBit[i] + "";
                                }
                            }
                            zoo = false;
                            Needzoo = false;
                        }
                        break;
                    case 9:
                        if (tmpVal == 0)
                        {
                            Needzoo = true;
                        }
                        else
                        {
                            strBit[i] = Cnum[tmpVal] + (bToUpper ? "" : "");
                            if (!HaveWan)
                            {
                                strBit[i] = strBit[i] + "";
                            }
                            HaveWan = true;
                            if (Needzoo)
                            {
                                if (zoo)
                                {
                                    strBit[i] = (strBit[3] == "" ? strBit[i] + (bMoney ? (bToUpper ? "" : "") : "") : strBit[i]);
                                }
                                else
                                {
                                    strBit[i] = strBit[i] + "";
                                }
                            }
                            zoo = false;
                            Needzoo = false;
                        }
                        break;
                    case 10:
                        if (tmpVal == 0)
                        {
                            Needzoo = true;
                        }
                        else
                        {
                            strBit[i] = Cnum[tmpVal] + (bToUpper ? "" : "");
                            if (!HaveWan)
                            {
                                strBit[i] = strBit[i] + "";
                            }
                            HaveWan = true;
                            if (Needzoo)
                            {
                                if (zoo)
                                {
                                    strBit[i] = (strBit[3] == "" ? strBit[i] + (bMoney ? (bToUpper ? "" : "") : "") : strBit[i]);
                                }
                                else
                                {
                                    strBit[i] = strBit[i] + "";
                                }
                            }
                            zoo = false;
                            Needzoo = false;
                        }
                        break;
                    case 11:
                        if (tmpVal == 0)
                        {
                            Needzoo = true;
                        }
                        else
                        {
                            strBit[i] = Cnum[tmpVal] + "亿";
                            if (Needzoo)
                            {
                                if (zoo)
                                {
                                    strBit[i] = (strBit[3] == "" ? strBit[i] + (bMoney ? (bToUpper ? "" : "") : "") : strBit[i]);
                                }
                                else
                                {
                                    strBit[i] = strBit[i] + "";
                                }
                            }
                            zoo = false;
                            Needzoo = false;
                            HaveYi = true;
                        }
                        break;
                    case 12:
                        if (tmpVal == 0)
                        {
                            Needzoo = true;
                        }
                        else
                        {
                            strBit[i] = Cnum[tmpVal] + (bToUpper ? "" : "");
                            if (!HaveYi)
                            {
                                strBit[i] = strBit[i] + "亿";
                            }
                            HaveYi = true;
                            if (Needzoo)
                            {
                                if (zoo)
                                {
                                    strBit[i] = (strBit[3] == "" ? strBit[i] + (bMoney ? (bToUpper ? "" : "") : "") : strBit[i]);
                                }
                                else
                                {
                                    strBit[i] = strBit[i] + "";
                                }
                            }
                            zoo = false;
                            Needzoo = false;
                        }
                        break;
                    case 13:
                        if (tmpVal == 0)
                        {
                            Needzoo = true;
                        }
                        else
                        {
                            strBit[i] = Cnum[tmpVal] + (bToUpper ? "" : "");
                            if (!HaveYi)
                            {
                                strBit[i] = strBit[i] + "亿";
                            }
                            HaveYi = true;
                            if (Needzoo)
                            {
                                if (zoo)
                                {
                                    strBit[i] = (strBit[3] == "" ? strBit[i] + (bMoney ? (bToUpper ? "" : "") : "") : strBit[i]);
                                }
                                else
                                {
                                    strBit[i] = strBit[i] + "";
                                }
                            }
                            zoo = false;
                            Needzoo = false;
                        }
                        break;
                    case 14:
                        if (tmpVal == 0)
                        {
                            Needzoo = true;
                        }
                        else
                        {
                            strBit[i] = Cnum[tmpVal] + (bToUpper ? "" : "");
                            if (!HaveYi)
                            {
                                strBit[i] = strBit[i] + "亿";
                            }
                            HaveYi = true;
                            if (Needzoo)
                            {
                                if (zoo)
                                {
                                    strBit[i] = (strBit[3] == "" ? strBit[i] + (bMoney ? (bToUpper ? "" : "") : "") : strBit[i]);
                                }
                                else
                                {
                                    strBit[i] = strBit[i] + "";
                                }
                            }
                            zoo = false;
                            Needzoo = false;
                        }
                        break;
                }
            }
            str = "";
            for (i = MaxLen - 1; i >= 0; i--)
            {
                str = str + strBit[i];
            }
            if (bMoney)
            {
                ConvertMoney = (str.Contains("") ? fuhao + str : fuhao + str + "");
            }
            else
            {
                if ((int)Value == 0)
                {
                    str = "";
                }
                if (sLast.Length > 0)
                {
                    if (Convert.ToInt32(sLast) > 0)
                    {
                        str = str + "";
                        for (i = 0; i < sLast.Length; i++)
                        {
                            str = str + Cnum[Convert.ToInt32(sLast.Substring(i, 1))];
                        }
                    }
                }
                ConvertMoney = fuhao + str;
            }
            return ConvertMoney;
        }

 

posted @ 2018-02-09 19:53  式圣2012  阅读(609)  评论(0编辑  收藏  举报