zoukankan      html  css  js  c++  java
  • 数字人民币(RMB)转化为大写汉字表达

    using System.Text;

    /// <summary>
    /// ToChineseValue 的摘要说明
    /// 人民币(RMB)转化为大写字母 
    /// </summary>
    public class ToChineseValue
    {
        private enum chineseChar { 零, 壹, 贰, 叁, 肆, 伍, 陆, 柒, 捌, 玖 };
        private enum charValue { 个 = 1, 十, 百, 千 };
        private enum c { 元 = 1, 万, 亿, 兆 };
        private enum afterPoint { 角, 分 };


        public ToChineseValue()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        public static string toChineseChar(decimal d)
        {
            decimal flag = 0;
            if (d < 0)
            {
                flag = d;
                d = -d;
            }

            string strChinese = "";
            string nextString = "";

            System.Globalization.NumberFormatInfo fmat = new System.Globalization.NumberFormatInfo();
            fmat.CurrencyDecimalDigits = 2;
            fmat.CurrencySymbol = "";
            fmat.CurrencyGroupSizes = new int[] { 4, 4, 4, 4 };
            fmat.CurrencyGroupSeparator = ",";

            string strx = d.ToString("c", fmat);
            string[] afterArray = strx.ToString().Split('.');

            char[] prePoint = afterArray[0].ToCharArray();
            char[] nextChar = afterArray[1].ToCharArray();

            if (System.Convert.ToDecimal(afterArray[0].ToString()) == 0)
            { strChinese = ""; }
            else
            {
                string[] str = afterArray[0].ToString().Split(',');
                int Num = str.Length;
                //交错数组用来放四个一组的数组
                char[][] part = new char[Num][];
                for (int i = 0; i < str.Length; i++)
                {
                    part[i] = str[i].ToCharArray();
                }
                for (int i = 0; i < Num; i++)
                {
                    for (int j = 0; j < part[i].Length; j++)
                    {
                        //用枚举完成汉字的转换
                        strChinese += ((chineseChar)int.Parse(part[i][j].ToString())).ToString();
                        //用枚举完成单位: 个 十 百 千 
                        strChinese += ((charValue)(part[i].Length - j)).ToString();
                    }
                    //以下为处理元 万 亿 兆 
                    strChinese += ((c)(part.Length - i)).ToString();
                }
            }
            //处理点号后面的小数部分        
            if (System.Convert.ToDecimal(afterArray[1].ToString()) == 0 && System.Convert.ToDecimal(afterArray[0].ToString()) != 0)
            {
                nextString = "整";
            }
            else
            {
                for (int i = 0; i < 2; i++)
                {
                    int t = int.Parse(nextChar[i].ToString());
                    nextString += ((chineseChar)int.Parse(nextChar[i].ToString())).ToString();
                    nextString += ((afterPoint)(i)).ToString();
                    if (t == 0)
                    {
                        StringBuilder str = new StringBuilder(nextString);
                        nextString = str.Replace("零零", "零").ToString();
                        nextString = str.Replace("零角零分", "零元").ToString();
                    }
                }
            }
            StringBuilder sb = new StringBuilder(strChinese);
            for (int i = 0; i < 4; i++)
            {
                strChinese = sb.Replace("个", "").ToString();
                strChinese = sb.Replace("零元", "元").ToString();
                strChinese = sb.Replace("零万", "万").ToString();
                strChinese = sb.Replace("亿万", "亿").ToString();
                strChinese = sb.Replace("零亿", "亿").ToString();
                strChinese = sb.Replace("零十", "零").ToString();
                strChinese = sb.Replace("零百", "零").ToString();
                strChinese = sb.Replace("零千", "零").ToString();
                strChinese = sb.Replace("零零", "零").ToString();
                strChinese = sb.Replace("零角零分", "整").ToString();
            }
            if (flag >= 0)
            {
                return strChinese + nextString;
            }
            return "负" + strChinese + nextString;
        }
    }

  • 相关阅读:
    日报11.1
    CCC2020 Surmising a Sprinter's Speed
    3D扫雷 (3D Minesweeper)
    如何使用小米手环与PN532(或类似芯片)复制验证卡号的IC卡
    分享一个api:随机二次元图片
    NOIP2017 时间复杂度 大模拟
    《区块链100问》笔记整理——42~49问
    Coursera-AndrewNg(吴恩达)机器学习笔记——第四周编程作业(多分类与神经网络)
    Coursera-AndrewNg(吴恩达)机器学习笔记——第四周
    《区块链100问》笔记整理——23~41问
  • 原文地址:https://www.cnblogs.com/jnhe/p/9529017.html
Copyright © 2011-2022 走看看