package com.gz.utils;

import cn.hutool.core.util.NumberUtil;

import java.math.BigDecimal;

/**
 * 单位Utils
 *
 * @author LiuChangLan
 * @since 2021/9/5 0:20
 */
public class UnitUtils {

    /**
     * 1KB
     */
    public static final Long KB = 1L * 1024;
    /**
     * 1MB
     */
    public static final Long MB = KB * 1024;
    /**
     * 1GB
     */
    public static final Long GB = MB * 1024;


    /**
     * 单位转换
     *
     * @author LiuChangLan
     * @since 2021/9/5 0:28
     */
    public static String storageUnitConvertStr(Long byteValue) {
        String value = "";
        if (byteValue >= KB && byteValue < MB) {
            value += NumberUtil.round(byteValue / 1.0 / KB, 2).toString();
            value += "KB";
        } else if (byteValue >= MB && byteValue < GB) {
            value += NumberUtil.round(byteValue / 1.0 / MB, 2).toString();
            value += "MB";
        } else if (byteValue >= GB) {
            value += NumberUtil.round(byteValue / 1.0 / GB, 2).toString();
            value += "GB";
        }
        return value;
    }

    public static BigDecimal storageUnitConvertMB(Long byteValue) {
        return NumberUtil.round(byteValue / 1.0 / MB, 2);
    }
}

