package com.mini.capi.utils;


import java.math.BigDecimal;
import java.math.RoundingMode;


public class vF {


    // 注意：起点是 KB，所以后面依次是 M、G、T、P
    private static final String[] UNITS = {"K", "M", "G", "T", "P"};
    private static final BigDecimal UNIT = BigDecimal.valueOf(1024L);

    public static String format(long kBytes) {
        if (kBytes < 0) {
            throw new IllegalArgumentException("KB 数不能为负数");
        }

        BigDecimal size = BigDecimal.valueOf(kBytes);
        int unitIndex = 0;

        while (size.compareTo(UNIT) >= 0 && unitIndex < UNITS.length - 1) {
            size = size.divide(UNIT, 2, RoundingMode.HALF_UP);
            unitIndex++;
        }

        // 去掉无意义的 0 和小数点
        String num = size.stripTrailingZeros().toPlainString();
        return num + UNITS[unitIndex];
    }


    public static boolean isNew(Object o) {
        return o == null;
    }

}
