码迷,mamicode.com
首页 > Windows程序 > 详细

C# 金额转换为中文

时间:2016-04-22 13:35:49      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                string s = Console.ReadLine();

                if (s == "0")
                {
                    break;
                }

                int n = s.Length;
                StringBuilder str = new StringBuilder();
                for (int i = 0; i < n; i++)
                {

                    if (int.Parse(s[i].ToString()) == 0)
                    {
                        if (n < 9&&n-i==5)
                        {
                            str.Append("万");

                        }//亿以内万位为零特殊处理
                        else
                        {
                            if (!str.ToString().EndsWith("零"))
                            {
                                str.Append("零");
                            }
                        }
                        
                       
                    }
                    else
                    {
                        str.Append(ConvertZh(int.Parse(s[i].ToString())));
                        str.Append(GetWei(n - i));

                    }

                }
                if (str.ToString().EndsWith("零"))
                {
                    str.Remove(str.Length-1, 1);
                }
                str.Replace("零万", "万");
                Console.WriteLine(str);
            }

            Console.ReadLine();
            ;

        }

        private static string ConvertZh(int a)
        {
            switch (a)
            {
                case 1:
                    return "壹";
                    break;

                case 2:
                    return "贰";
                    break;
                case 3:
                    return "叁";
                    break;
                case 4:
                    return "肆";
                    break;
                case 5:
                    return "伍";
                    break;
                case 6:
                    return "陆";
                    break;
                case 7:
                    return "柒";
                    break;
                case 8:
                    return "捌";
                    break;
                case 9:
                    return "玖";
                    break;



            }
            return "";

        }
        private static string GetWei(int a)
        {
            switch (a)
            {

                case 2:
                    return "拾";
                    break;
                case 3:
                    return "佰";
                    break;
                case 4:
                    return "仟";
                    break;
                case 5:
                    return "万";
                    break;
                case 6:
                    return "拾";
                    break;
                case 7:
                    return "佰";
                    break;
                case 8:
                    return "仟";
                    break;
                case 9:
                    return "亿";
                    break;



            }
            return "";

        }

    }
}

C# 金额转换为中文

标签:

原文地址:http://www.cnblogs.com/cos0930/p/5420761.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!