zoukankan      html  css  js  c++  java
  • [原创] 用C#将数字转换为人民币大写

    网上已经有一些数字转换为人民币大写的源码和控件,但还是自己动手写了一个。一是觉得好玩,二是希望能给需要的朋友提供一些方便:

      1    public class XConvert
      2    {
      3        public static string ToRMB(Double e)
      4        {
      5            return ToRMB(System.Convert.ToDecimal(e));
      6        }

      7
      8        public static string ToRMB(Decimal e)
      9        {
     10            string eString;//数字的格式化字符串
     11            string eNum;//单数字
     12            int eLen;//格式化字符串长度
     13
     14            System.Text.StringBuilder rmb=new System.Text.StringBuilder();//人民币大写
     15            string yuan;//
     16            bool seriesZero;//连续0标志
     17            bool minus=false;//负数标志
     18
     19            if (e==0m)
     20            {
     21                return "零圆整";
     22            }

     23            if (e<0m)
     24            {
     25                minus=true;
     26                e=System.Math.Abs(e);                
     27            }

     28            if (e>999999999999.99m)
     29            {
     30                throw new Exception("超过最大范围");
     31            }

     32
     33            eString=e.ToString("0.00");
     34            eLen=eString.Length;
     35            yuan=(eString.Substring(0,1)=="0"?"":"");
     36
     37            eNum=eString.Substring(eLen-1,1);//分位
     38            if (eNum=="0")
     39            {
     40                rmb.Append("");
     41                seriesZero=true;
     42            }

     43            else
     44            {
     45                rmb.Append(stringNum(eNum)+"");
     46                seriesZero=false;
     47            }

     48
     49            eNum=eString.Substring(eLen-2,1);//角位
     50            if (eNum=="0")
     51            {
     52                if (!seriesZero)
     53                {
     54                    if (!(eLen==4&&eString.Substring(0,1)=="0"))
     55                    {
     56                        rmb.Insert(0,"");
     57                    }

     58                }

     59            }

     60            else
     61            {
     62                rmb.Insert(0,stringNum(eNum)+"");            
     63                seriesZero=false;
     64            }

     65
     66            if (eLen<=7)
     67            {
     68                rmb.Insert(0,stringNum4(eString.Substring(0,eLen-3))+yuan);
     69            }

     70            else if (eLen<=11)
     71            {
     72                rmb.Insert(0,stringNum4(eString.Substring(eLen-7,4))+yuan);
     73                rmb.Insert(0,stringNum4(eString.Substring(0,eLen-7))+"");
     74            }

     75            else if (eLen<=15)
     76            {
     77                rmb.Insert(0,stringNum4(eString.Substring(eLen-7,4))+yuan);
     78                rmb.Insert(0,stringNum4(eString.Substring(eLen-11,4))+(eString.Substring(eLen-11,4)=="0000"?"":""));
     79                rmb.Insert(0,stringNum4(eString.Substring(0,eLen-11))+"亿");
     80            }

     81
     82            if (minus) rmb.Insert(0,"");
     83
     84            return rmb.ToString();
     85        }

     86
     87        private static string stringNum4(string eNum4)
     88        {
     89            string eNum;
     90            bool seriesZero=false;
     91            System.Text.StringBuilder rmb4=new System.Text.StringBuilder();
     92            int eLen=eNum4.Length;
     93
     94            eNum=eNum4.Substring(eLen-1,1);//个位
     95            if (eNum=="0")
     96            {
     97                seriesZero=true;
     98            }

     99            else
    100            {
    101                rmb4.Append(stringNum(eNum));
    102            }

    103
    104            if (eLen>=2)//十位
    105            {
    106                eNum=eNum4.Substring(eLen-2,1);
    107                if (eNum=="0")
    108                {
    109                    if (!seriesZero)
    110                    {
    111                        rmb4.Insert(0,"");
    112                        seriesZero=true;
    113                    }

    114                }

    115                else
    116                {
    117                    rmb4.Insert(0,stringNum(eNum)+"");
    118                    seriesZero=false;
    119                }

    120            }

    121
    122            if (eLen>=3)//百位
    123            {
    124                eNum=eNum4.Substring(eLen-3,1);
    125                if(eNum=="0")
    126                {
    127                    if (!seriesZero)
    128                    {
    129                        rmb4.Insert(0,"");
    130                        seriesZero=true;
    131                    }

    132                }

    133                else
    134                {
    135                    rmb4.Insert(0,stringNum(eNum)+"");
    136                    seriesZero=false;
    137                }

    138            }

    139
    140            if (eLen==4)//千位
    141            {
    142                eNum=eNum4.Substring(0,1);
    143                if(eNum=="0")
    144                {
    145                    if (!seriesZero)
    146                    {
    147                        rmb4.Insert(0,"");
    148                        seriesZero=true;
    149                    }

    150                }

    151                else
    152                {
    153                    rmb4.Insert(0,stringNum(eNum)+"");
    154                    seriesZero=false;
    155                }

    156            }

    157
    158            return rmb4.ToString();
    159        }

    160
    161        private static string stringNum(string eNum)
    162        {
    163            switch (eNum)
    164            {
    165                case "1":
    166                    return "";
    167                case "2":
    168                    return "";
    169                case "3":
    170                    return "";
    171                case "4":
    172                    return "";
    173                case "5":
    174                    return "";
    175                case "6":
    176                    return "";
    177                case "7":
    178                    return "";
    179                case "8":
    180                    return "";
    181                case "9":
    182                    return "";
    183                default:
    184                    return "";
    185            }

    186        }

    187
    不足之处,望朋友们指正。
  • 相关阅读:
    与众不同 windows phone (50)
    与众不同 windows phone (49)
    重新想象 Windows 8.1 Store Apps (93)
    重新想象 Windows 8.1 Store Apps 系列文章索引
    重新想象 Windows 8.1 Store Apps (92)
    重新想象 Windows 8.1 Store Apps (91)
    重新想象 Windows 8.1 Store Apps (90)
    重新想象 Windows 8.1 Store Apps (89)
    重新想象 Windows 8.1 Store Apps (88)
    重新想象 Windows 8.1 Store Apps (87)
  • 原文地址:https://www.cnblogs.com/chinadhf/p/296125.html
Copyright © 2011-2022 走看看