计算机硬盘大小转换(B,KB,MB,GB,TB,PB之间的大小转换)

简介: <p>程序员都很懒,你懂的!</p> <p>java程序员在实际的开发中会遇到很多的单位换算问题。今天我给大家带来的是关于计算机硬盘大小的换算。多数情况下,一般要求b,kb,mb,gb,tb,pb之间的大小转换,我们都知道他们之间的换算是乘以1024或者除以1024。但是具体怎么用java代码来实现呢?请看下面的代码:</p> <p></p> <pre code_snippet_id

程序员都很懒,你懂的!

java程序员在实际的开发中会遇到很多的单位换算问题。今天我给大家带来的是关于计算机硬盘大小的换算。多数情况下,一般要求b,kb,mb,gb,tb,pb之间的大小转换,我们都知道他们之间的换算是乘以1024或者除以1024。但是具体怎么用java代码来实现呢?请看下面的代码:

package com.herman.utils;

/***
 * @see 存储大小(单位)转换器.
 * @author Herman.Xiong
 * @date 2014年5月27日 13:27:40
 * @version V1.0
 */
public enum SizeConverter {
    /** 转换任意单位的大小, 返回结果会包含两位小数但不包含单位. */
    Arbitrary {
        @Override
        public String convert(float size) {
            while (size > 1024) {
                size /= 1024;
            }
            return String.format(FORMAT_F, size);
        }
    },
    
    // -----------------------------------------------------------------------
    // 有单位
    /** 转换单位为B的大小, 返回结果会包含两位小数以及单位. 如: 1024B->1KB, (1024*1024)B->1MB */
    B {
        @Override
        public String convert(float B) {
            return converter(0, B);
        }
    },
    /** 转换单位为B的大小, 返回结果会包含两位小数以及单位. */
    KB {
        @Override
        public String convert(float KB) {
            return converter(1, KB);
        }
    },
    /** 转换单位为MB的大小, 返回结果会包含两位小数以及单位. */
    MB {
        @Override
        public String convert(float MB) {
            return converter(2, MB);
        }
    },
    /** 转换单位为GB的大小, 返回结果会包含两位小数以及单位. */
    GB {
        @Override
        public String convert(float GB) {
            return converter(3, GB);
        }
    },
    /** 转换单位为TB的大小, 返回结果会包含两位小数以及单位. */
    TB {
        @Override
        public String convert(float TB) {
            return converter(4, TB);
        }
    },
    
    // -----------------------------------------------------------------------
    // trim没单位
    /** 转换任意单位的大小, 返回结果小数部分为0时将去除两位小数, 不包含单位. */
    ArbitraryTrim {
        @Override
        public String convert(float size) {
            while (size > 1024) {
                size /= 1024;
            }

            int sizeInt = (int) size;
            boolean isfloat = size - sizeInt > 0.0F;
            if (isfloat) {
                return String.format(FORMAT_F, size);
            }
            return String.format(FORMAT_D, sizeInt);
        }
    },
    
    // -----------------------------------------------------------------------
    // trim有单位
    /** 转换单位为B的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位. */
    BTrim {
        @Override
        public String convert(float B) {
            return trimConverter(0, B);
        }
    },
    /** 转换单位为KB的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位. */
    KBTrim {
        @Override
        public String convert(float KB) {
            return trimConverter(1, KB);
        }
    },
    /** 转换单位为MB的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位. */
    MBTrim {
        @Override
        public String convert(float MB) {
            return trimConverter(2, MB);
        }
    },
    /** 转换单位为GB的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位. */
    GBTrim {
        @Override
        public String convert(float GB) {
            return trimConverter(3, GB);
        }
    },
    /** 转换单位为TB的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位. */
    TBTrim {
        @Override
        public String convert(float TB) {
            return trimConverter(4, TB);
        }
    };
    /***
     * <p> 将指定的大小转换到1024范围内的大小. 注意该方法的最大单位为PB, 最小单位为B, 
     * 任何超出该范围的单位最终会显示为**. </p>
     * 
     * @param size 要转换的大小, 注意是浮点数, 不要以整形的方式传入, 容易造成溢出.
     *         (如: 1024*1024*1024*1024*1024会溢出, 使结果为0, 因为它先将结果以int相乘后再转换为float; 
     *         而1024.0F*1024.0F*1024.0F*1024.0F*1024.0F就不会溢出)
     * @return
     */
    abstract public String convert(float size);
    
    // -----------------------------------------------------------------------
    // 单位转换
    
    private static final String[] UNITS = new String[] {
        "B", "KB", "MB", "GB", "TB", "PB", "**"
    };
    
    private static final int LAST_IDX = UNITS.length-1;
    
    private static final String FORMAT_F = "%1$-1.2f";
    private static final String FORMAT_F_UNIT = "%1$-1.2f%2$s";
    
    private static final String FORMAT_D = "%1$-1d";
    private static final String FORMAT_D_UNIT = "%1$-1d%2$s";
    
    // -----------------------------------------------------------------------
    private static String converter(int unit, float size) {
        int unitIdx = unit;
        while (size > 1024) {
            unitIdx++;
            size /= 1024;
        }
        int idx = unitIdx < LAST_IDX ? unitIdx : LAST_IDX;
        return String.format(FORMAT_F_UNIT, size, UNITS[idx]);
    }
    
    private static String trimConverter(int unit, float size) {
        int unitIdx = unit;
        while (size > 1024) {
            unitIdx++;
            size /= 1024;
        }

        int sizeInt = (int) size;
        boolean isfloat = size - sizeInt > 0.0F;
        int idx = unitIdx < LAST_IDX ? unitIdx : LAST_IDX;
        if (isfloat) {
            return String.format(FORMAT_F_UNIT, size, UNITS[idx]);
        }
        return String.format(FORMAT_D_UNIT, sizeInt, UNITS[idx]);
    }
    
    // -----------------------------------------------------------------------
    public static String convertBytes(float B, boolean trim) {
        return trim ? trimConvert(0, B, true) : convert(0, B, true);
    }
    
    public static String convertKB(float KB, boolean trim) {
        return trim ? trimConvert(1, KB, true) : convert(1, KB, true);
    }
    
    public static String convertMB(float MB, boolean trim) {
        return trim ? trimConvert(2, MB, true) : convert(2, MB, true);
    }
    
    /***
     * <p> 存储大小单位间的转换. 注意该方法的最大单位为PB, 最小单位为B, 
     * 任何超出该范围的单位最终会显示为**. </p>
     * 
     * @param unit 从哪个单位开始
     * @param size 存储大小, 注意是float, 不要以整形的形式传入, 否则会溢出(如:1024*1024这种,
     * 它是先将1024*1024作为int相乘再转换为float的, 如果值过大的话就会溢出了, 
     * 所以这么写1024.0F*1024.0F)
     * @param withUnit 返回的结果字符串是否带有对应的单位
     * @return
     */
    private static String convert(int unit, float size, boolean withUnit) {
        int unitIdx = unit;
        while (size > 1024) {
            unitIdx++;
            size /= 1024;
        }
        if (withUnit) {
            int idx = unitIdx < LAST_IDX ? unitIdx : LAST_IDX;
            return String.format(FORMAT_F_UNIT, size, UNITS[idx]);
        }
        return String.format(FORMAT_F, size);
    }
    
    /***
     * <p> 存储大小单位间的转换, 如果转换后小数部分为0, 则去除小数部分. 
     * 注意该方法的最大单位为PB, 最小单位为B, 任何超出该范围的单位最终会显示为**. </p>
     * 
     * @param unit 从哪个单位开始
     * @param size 存储大小, 注意是float, 不要以整形的形式传入, 否则会溢出(如:1024*1024这种,
     * 它是先将1024*1024作为int相乘再转换为float的, 如果值过大的话就会溢出了, 
     * 所以这么写1024.0F*1024.0F)
     * @param withUnit 返回的结果字符串是否带有对应的单位
     * @return
     */
    private static String trimConvert(int unit, float size, boolean withUnit) {
        int unitIdx = unit;
        while (size > 1024) {
            unitIdx++;
            size /= 1024;
        }

        int sizeInt = (int) size;
        boolean isfloat = size - sizeInt > 0.0F;
        if (withUnit) {
            int idx = unitIdx < LAST_IDX ? unitIdx : LAST_IDX;
            if (isfloat) {
                return String.format(FORMAT_F_UNIT, size, UNITS[idx]);
            }
            return String.format(FORMAT_D_UNIT, sizeInt, UNITS[idx]);
        }

        if (isfloat) {
            return String.format(FORMAT_F, size);
        }
        return String.format(FORMAT_D, sizeInt);
    }
}

工具类代码写好了,我们来看一个测试类吧,上代码:

package com.herman.test;

import com.herman.utils.SizeConverter;
/**
 * @see 硬盘大小换算测试类
 * @author Herman.Xiong
 * @date 2014年5月27日 13:43:33
 */
public class SizeConverterTest {
	public static void main(String[] args) {
		System.out.println(SizeConverter.MBTrim.convert(419562f));
	}
}
好了,就到这里了,如果想下载更详细的内容,请 点击下载:http://download.csdn.net/detail/xmt1139057136/7407229

或者加入QQ群:135430763共同学习!

目录
相关文章
|
7月前
|
编解码 网络协议 开发工具
GB/T28181-2022协议版本标识X-GB-Ver解读
GB28181-2022相对2016,其中有个变化是:报文中携带协议版本标识 X-GB-Ver:3.0(3.0-2022 2.0-2016)
|
3月前
|
算法 C#
C# .Net Core bytes转换为GB/MB/KB 算法
C# .Net Core bytes转换为GB/MB/KB 算法
34 0
|
9月前
|
PHP
php常用自建函数学习(2):kb/mb/gb/tb单位数据大小自动转换
php常用自建函数学习(2):kb/mb/gb/tb单位数据大小自动转换
66 0
|
6月前
|
存储 人工智能 内存技术
PNI800 MB805 CTB810 HN800 DSTC190
PNI800 MB805 CTB810 HN800 DSTC190
27 0
Golang:go-humanize将文件大小转换成Kb、Mb、Gb适合人类阅读的单位
Golang:go-humanize将文件大小转换成Kb、Mb、Gb适合人类阅读的单位
236 0
|
存储
[oeasy]python0088_字节_Byte_存储单位_KB_MB_GB_TB
[oeasy]python0088_字节_Byte_存储单位_KB_MB_GB_TB
115 0
[oeasy]python0088_字节_Byte_存储单位_KB_MB_GB_TB
|
存储 弹性计算 固态存储
阿里云存储价格100GB/1TB/50TB/500GB空间费用大全
阿里云云存储怎么收费?云存储1GB空间容量多少钱?如果是OSS对象存储1GB价格是0.12元/GB/月,标准存储包1TB一年价格999元;NAS文件存储100GB价格是30元一个月,500G空间150元/月;块存储高效云盘40G空间14元/月
14623 0
阿里云存储价格100GB/1TB/50TB/500GB空间费用大全
|
存储
单位换算】存储单位(bit Byte KB MB GB TB PB EB ZB YB BB)时间单位(ms μs ns ps)长度单位(dm cm mm μm nm pm fm am zm ym)
单位换算】存储单位(bit Byte KB MB GB TB PB EB ZB YB BB)时间单位(ms μs ns ps)长度单位(dm cm mm μm nm pm fm am zm ym)
374 0