文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>阿拉伯數字換中文小寫

阿拉伯數字換中文小寫

时间:2010-09-02  来源:风飘零

   写东西的时候,由于要用到。准备在网上找的,不过不是自己要求的那种类型。比如我在网上找的:10:一十零,11:一十一,很不好。所以我就自己写了一个。希望能帮到大家。虽然是有点那么的繁琐。不过这个是转换万(包含万)以内的int 数据。注:本段代码是在C#里面用的。其他语言应该也可以用。

        /// <summary>
        /// 阿拉伯數字轉中文
        /// </summary>
        /// <param name="intInput"></param>
        /// <returns></returns>
        public static string ToConvtZH(int intInput)
        {
            string sd = ToCH(intInput);
            string sd2 = sd;
            if (intInput.ToString().Length > 2 && ((intInput / 10 % 10) == 1))
            {
                sd2 = sd.Insert(sd.LastIndexOf('十'), "一");
            }
            return sd2;
        }

        public static string ToCH(int intInput)
        {
            string si = intInput.ToString();
            string sd = "";
            if (si.Length == 1)     //個
            {
                sd += GetCH(intInput);
                return sd;
            }
            else if (si.Length == 2)//十
            {
                if (si.Substring(0, 1) == "1")
                    sd += "十";
                else
                    sd += (GetCH(intInput / 10) + "十");
                sd += ToCH(intInput % 10);
            }
            else if (si.Length == 3)//百
            {
                sd += (GetCH(intInput / 100) + "百");
                if ((intInput % 100).ToString().Length < 2)
                    sd += "零";
                sd += ToCH(intInput % 100);
            }
            else if (si.Length == 4)//千
            {
                sd += (GetCH(intInput / 1000) + "千");
                if ((intInput % 1000).ToString().Length < 3)
                    sd += "零";
                sd += ToCH(intInput % 1000);
            }
            else if (si.Length == 5)//萬
            {
                sd += (GetCH(intInput / 10000) + "萬");
                if ((intInput % 10000).ToString().Length < 4)
                    sd += "零";
                sd += ToCH(intInput % 10000);
            }

            return sd;
        }

        private static string GetCH(int input)
        {
            string sd = "";
            switch (input)
            {
                case 1:
                    sd = "一";
                    break;
                case 2:
                    sd = "二";
                    break;
                case 3:
                    sd = "三";
                    break;
                case 4:
                    sd = "四";
                    break;
                case 5:
                    sd = "五";
                    break;
                case 6:
                    sd = "六";
                    break;
                case 7:
                    sd = "七";
                    break;
                case 8:
                    sd = "八";
                    break;
                case 9:
                    sd = "九";
                    break;
                default:
                    break;
            }
            return sd;
        }

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载