zoukankan      html  css  js  c++  java
  • C#将数据大小字节转换为MB,GB,TB

    http://www.myluoluo.com/c%E5%B0%86%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%E5%AD%97%E8%8A%82%E8%BD%AC%E6%8D%A2%E4%B8%BAmb-gb-tb.love

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConvertDataSize
    {
        public class Convert
        {
            /// <summary>
            /// 获取
            /// </summary>
            /// <param name="b"></param>
            /// <returns></returns>
            public string GetSize(long b)
            {
                if (b.ToString().Length <= 10)
                    return GetMB(b);
                if (b.ToString().Length >= 11 && b.ToString().Length <=12)
                    return GetGB(b);
                if (b.ToString().Length >= 13)
                    return GetTB(b);
                return String.Empty;
            }
    
            /// <summary>
            /// 将B转换为TB
            /// </summary>
            /// <param name="b"></param>
            /// <returns></returns>
            private string GetTB(long b)
            {
                for (int i = 0; i < 4; i++)
                {
                    b /= 1024;
                }
                return b + "TB";
            }
    
            /// <summary>
            /// 将B转换为GB
            /// </summary>
            /// <param name="b"></param>
            /// <returns></returns>
            private string GetGB(long b)
            {
                for (int i = 0; i < 3; i++)
                {
                    b /= 1024;
                }
                return b + "GB";
            }
    
            /// <summary>
            /// 将B转换为MB
            /// </summary>
            /// <param name="b"></param>
            /// <returns></returns>
            private string GetMB(long b)
            {
                for (int i = 0; i < 2; i++)
                {
                    b /= 1024;
                }
                return b + "MB";
            }
        }
    }
  • 相关阅读:
    Lua笔记
    psp info
    防火墙选项变成灰色解决办法
    server error.分析器错误信息: 未能加载类型. line 1
    [C++][stl]vector、list、deque
    恢复隐藏文件ShowAll.reg
    C++笔记
    C#成员的继承
    C#运算符重载
    .NET每个开发人员现在应该下载的十种必备工具
  • 原文地址:https://www.cnblogs.com/yibinboy/p/4159653.html
Copyright © 2011-2022 走看看