package com.storlead.sales.mail.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2024-08-09 18:33
 */

public class FileHelper {

    /**
     * 获取文件大小
     *
     * @author timo
     * @date 2018/9/3 14:03
     */
    public static String formatFileSize(Long size) {
        if (Objects.isNull(size)) {
            return "0 B";
        }
        if (Long.valueOf(0).equals(size)) {
            return "0 B";
        }
        if (size < 0) {
            return "0 B";
        }
        if (size < 1024) {
            return size + " B";
        } else if (size < 1024 * 1024) {
            return String.format("%.2f KB", size / 1024.0);
        } else if (size < 1024 * 1024 * 1024) {
            return String.format("%.2f MB", size / (1024.0 * 1024));
        } else {
            return String.format("%.2f GB", size / (1024.0 * 1024 * 1024));
        }
    }
}
