管理

版本号处理工具类 - C#小函数类推荐

Posted on 2024-11-27 19:59  lzhdim  阅读(5134)  评论(0编辑  收藏  举报

       此文记录的是版本号操作的类。

/***

    版本号处理工具类

    Austin Liu 刘恒辉
    Project Manager and Software Designer

    E-Mail: lzhdim@163.com
    Blog:   http://lzhdim.cnblogs.com
    Date:   2024-01-15 15:18:00

    使用方法:
        VersionUtil.Convert(iRevision, 4);

***/

namespace Lzhdim.LPF.Utility
{
    /// <summary>
    /// 版本号处理工具类
    /// </summary>
    public class VersionUtil
    {
        /// <summary>
        /// 将版本号转换为指定长度的字符串
        /// </summary>
        /// <param name="version">版本号整型值</param>
        /// <param name="digitCount">字符串长度值</param>
        /// <returns>格式化后的值</returns>
        public static string Convert(int version, int digitCount)
        {
            int iCount = GetDigitCount(version);

            string retString = string.Empty;
            if (iCount < digitCount)
            {
                retString = 0.ToString($"D{digitCount - iCount}");
            }

            return retString + version.ToString();
        }

        /// <summary>
        /// 计算该整型值有几位数
        /// </summary>
        /// <param name="number">整型值</param>
        /// <returns>位数</returns>
        private static int GetDigitCount(int number)
        {
            int count = 0;
            do
            {
                count++;
                number /= 10;
            } while (number > 0);
            return count;
        }
    }
}

 

Copyright © 2000-2022 Lzhdim Technology Software All Rights Reserved