首页 > 代码库 > C#将数据大小字节转换为MB,GB,TB

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";        }    }}

 

C#将数据大小字节转换为MB,GB,TB