首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。