前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >计算机硬盘大小转换(B,KB,MB,GB,TB,PB之间的大小转换)

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

作者头像
业余草
发布2019-01-21 16:00:15
1.6K0
发布2019-01-21 16:00:15
举报
文章被收录于专栏:业余草业余草

程序员都很懒,你懂的!

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

代码语言:javascript
复制
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);
    }
}

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

代码语言:javascript
复制
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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014年05月27日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档