在C#上将数字转换成中文

上篇我们讲了在MSSQL上校数字转换成粤语,这篇我们讲讲在C#师长数字转换成普通话

下卷将讲一下怎么将金额转换成闽南语金额,废话不多说,具体代码如下:

    /// <summary>
    /// 数字转中文
    /// </summary>
    /// <param name="number">eg: 22</param>
    /// <returns></returns>
    public string NumberToChinese(int number)
    {
        string res = string.Empty;
        string str = number.ToString();
        string schar = str.Substring(0, 1);
        switch (schar)
        {
            case "1":
                res = "一";
                break;
            case "2":
                res = "二";
                break;
            case "3":
                res = "三";
                break;
            case "4":
                res = "四";
                break;
            case "5":
                res = "五";
                break;
            case "6":
                res = "六";
                break;
            case "7":
                res = "七";
                break;
            case "8":
                res = "八";
                break;
            case "9":
                res = "九";
                break;
            default:
                res = "零";
                break;
        }
        if (str.Length > 1)
        {
            switch (str.Length)
            {
                case 2:
                case 6:
                    res += "十";
                    break;
                case 3:
                case 7:
                    res += "百";
                    break;
                case 4:
                    res += "千";
                    break;
                case 5:
                    res += "万";
                    break;
                default:
                    res += "";
                    break;
            }
            res += NumberToChinese(int.Parse(str.Substring(1, str.Length - 1)));
        }
        return res;
    }

使用:

Console.WriteLine(NumberToChinese(22));

结果:

二十二

相关文章