字节转换工具

/**
 * 字节转换工具
 * @author ThinkGem
 * @version 2015-6-20
 */
public class ByteUtils {

    private static final int UNIT = 1024;

    /**
     * @param byteSize 字节
     * @return
     */
    public static String formatByteSize(long byteSize) {

        double size = 1.0 * byteSize;

        String type = "B";
        if((int)Math.floor(size / UNIT) <= 0) { //不足1KB
            type = "B";
            return format(size, type);
        }

        size = size / UNIT;
        if((int)Math.floor(size / UNIT) <= 0) { //不足1MB
            type = "KB";
            return format(size, type);
        }

        size = size / UNIT;
        if((int)Math.floor(size / UNIT) <= 0) { //不足1GB
            type = "MB";
            return format(size, type);
        }

        size = size / UNIT;
        if((int)Math.floor(size / UNIT) <= 0) { //不足1TB
            type = "GB";
            return format(size, type);
        }

        size = size / UNIT;
        if((int)Math.floor(size / UNIT) <= 0) { //不足1PB
            type = "TB";
            return format(size, type);
        }

        size = size / UNIT;
        if((int)Math.floor(size / UNIT) <= 0) {
            type = "PB";
            return format(size, type);
        }
        return ">PB";
    }

    private static String format(double size, String type) {
        int precision = 0;

        if(size * 1000 % 10 > 0) {
            precision = 3;
        } else if(size * 100 % 10 > 0) {
            precision = 2;
        } else if(size * 10 % 10 > 0) {
            precision = 1;
        } else {
            precision = 0;
        }

        String formatStr = "%." + precision + "f";

        if("KB".equals(type)) {
            return String.format(formatStr, (size)) + "KB";
        } else if("MB".equals(type)) {
            return String.format(formatStr, (size)) + "MB";
        } else if("GB".equals(type)) {
            return String.format(formatStr, (size)) + "GB";
        } else if("TB".equals(type)) {
            return String.format(formatStr, (size)) + "TB";
        } else if("PB".equals(type)) {
            return String.format(formatStr, (size)) + "PB";
        }
        return  String.format(formatStr, (size)) + "B";
    }

    public static void main(String[] args) {
        System.out.println(ByteUtils.formatByteSize(1023));
        System.out.println(ByteUtils.formatByteSize(1L * UNIT));
        System.out.println(ByteUtils.formatByteSize(1L * UNIT * UNIT));
        System.out.println(ByteUtils.formatByteSize(1L * UNIT * 1023));
        System.out.println(ByteUtils.formatByteSize(1L * 1023 * 1023 * 1023));
        System.out.println(ByteUtils.formatByteSize(1L * UNIT * UNIT * UNIT));
        System.out.println(ByteUtils.formatByteSize(1L * UNIT * UNIT * UNIT * UNIT));
        System.out.println(ByteUtils.formatByteSize(1L * UNIT * UNIT * UNIT * UNIT * UNIT));
        System.out.println(ByteUtils.formatByteSize(1L * UNIT * UNIT * UNIT * UNIT * UNIT * UNIT));
    }

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值