zoukankan      html  css  js  c++  java
  • 订单号生成规则,谁用谁知道

     1 class Program
     2     {
     3         //订单号数据源,实际项目中,这里是从数据库中读取过来的
     4         string[] strList = { "201512000001", "201512000002", "201512000003", "201512000004", 
     5                              "201512000008", "201512000007", "201512000006", "201512000005", 
     6                              "201512000009", "201512000010", "201512000011", "201512000012",
     7                              "201512000013","201512000014","201512000015" };        
     8         static void Main(string[] args)
     9         {
    10             Program p = new Program();
    11             Console.WriteLine(p.GetOrderNum());
    12             Console.ReadKey();
    13         }
    14 
    15         public string GetOrderNum()
    16         {
    17             string result = "";            
    18             string YearMonth = DateTime.Now.ToString("yyyyMM");
    19             var count = Count(YearMonth);//201512  5
    20             result = YearMonth + "0000001";
    21             var dd = count.ToString().Length;
    22             switch (dd)
    23             {
    24                 case 1:
    25                     result = YearMonth + "00000" + (count + 1);
    26                     break;
    27                 case 2:
    28                     result = YearMonth + "0000" + (count + 1);
    29                     break;
    30                 case 3:
    31                     result = YearMonth + "000" + (count + 1);
    32                     break;
    33                 case 4:
    34                     result = YearMonth + "00" + (count + 1);
    35                     break;
    36                 case 5:
    37                     result = YearMonth + "0" + (count + 1);
    38                     break;
    39                 default:
    40                     result = YearMonth + (count + 1);
    41                     break;
    42             }
    43             return result;
    44         }
    45         
    46         public int Count(string yearMonth) {
    47             int result = 0;
    48             result = strList.Count(item=>item.Contains(yearMonth));
    49             return result;
    50         }       
    51     }

    用string.PadLeft(n,'0')补零写出来的代码要比上面if的要好看很多,多谢园友提醒,最终代码请看下面

    public string GetOrderNum2()
    {
    string result = "";
    string YearMonth = DateTime.Now.ToString("yyyyMM");
    var count = Count(YearMonth);//201512 5 
    result= YearMonth+(count + 1).ToString().PadLeft(6,'0');
    return result;
    }
    

      

    谁有更好的生成规则,不妨一起讨论下

  • 相关阅读:
    python_16(bootstrap)
    python_15(jquery)
    python_14(js)
    .net 定义泛型方法解析XML数据赋值给相应对象
    SQL Server 数字字符串位数不够补0
    SQL Server 跨服务器查询
    JQ1.5 为动态追加的元素添加事件
    radio group 的change 事件
    记录兼职工作中遇到的问题-IIS 服务器站点无法启动
    记录第一份工作的最后一次需求-百分比环形进度条
  • 原文地址:https://www.cnblogs.com/axinno1/p/5025268.html
Copyright © 2011-2022 走看看