数据存储单位转换,b、byte、KB、MB、GB、TB转换工具类

package com.sv.admin.commons.utils;

import java.math.BigDecimal;

/**
 * 存储单位换算
 *
 * @author Gltqe
 * @date 2022/7/3 0:31
 **/
public class StorageUnitUtil {

    public static final Long BIT = 1L;
    public static final Long BYTE = 8L;
    public static final Long KB = 8 * 1024L;
    public static final Long MB = 8 * 1024 * 1024L;
    public static final Long GB = 8 * 1024 * 1024 * 1024L;
    public static final Long TB = 8 * 1024 * 1024 * 1024L * 1024L;
    public static final Integer SCALE = 2;

    /**
     * 存储单位换算
     *
     * @param length      需要转换的存储大小
     * @param currentUnit 当前存储单位
     * @param targetUnit  转换目标存储单位
     * @param scale       小数点
     * @return java.math.BigDecimal
     * @author Gltqe
     * @date 2022/7/3 0:32
     **/
    public static BigDecimal convert(BigDecimal length, Long currentUnit, Long targetUnit, Integer scale) {
        if (scale == null) {
            scale = SCALE;
        }
        if (currentUnit < targetUnit) {
            BigDecimal b1 = new BigDecimal(targetUnit / currentUnit);
            BigDecimal decimal = length.divide(b1, scale, BigDecimal.ROUND_HALF_UP);
            return decimal;
        } else if (currentUnit > targetUnit) {
            BigDecimal b1 = new BigDecimal(currentUnit / targetUnit);
            BigDecimal decimal = length.multiply(b1).setScale(scale, BigDecimal.ROUND_HALF_UP);
            return decimal;
        } else {
            return length;
        }
    }

    public static void main(String[] args) {
        // 测试 : 1500KB 转为 MB // 结果: 1.465
        BigDecimal convert1 = convert(new BigDecimal("1500"), KB, MB, 3);
        System.out.println(convert1);
        // 测试 : 1500b 转为 byte // 结果: 187.500
        BigDecimal convert2 = convert(new BigDecimal("1500"), BIT,BYTE, 3);
        System.out.println(convert2);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是Java中用于KB/MB/GB转换工具类代码示例: ``` public class ConvertUtils { private static final long KB = 1024; private static final long MB = KB * 1024; private static final long GB = MB * 1024; public static String byteToReadable(long bytes) { if (bytes >= GB) { return String.format("%.2f GB", (float) bytes / GB); } else if (bytes >= MB) { return String.format("%.2f MB", (float) bytes / MB); } else if (bytes >= KB) { return String.format("%.2f KB", (float) bytes / KB); } else { return String.format("%d Bytes", bytes); } } public static long toByte(String size) { if (size == null || "".equals(size.trim())) { return 0; } size = size.toUpperCase(Locale.ENGLISH); String unit = "B"; if (size.endsWith("KB")) { unit = "KB"; size = size.substring(0, size.length() - 2).trim(); } else if (size.endsWith("MB")) { unit = "MB"; size = size.substring(0, size.length() - 2).trim(); } else if (size.endsWith("GB")) { unit = "GB"; size = size.substring(0, size.length() - 2).trim(); } else if (size.endsWith("B")) { size = size.substring(0, size.length() - 1).trim(); } double value = Double.parseDouble(size); if ("KB".equals(unit)) { value *= KB; } else if ("MB".equals(unit)) { value *= MB; } else if ("GB".equals(unit)) { value *= GB; } return (long) value; } } ``` 使用方法: ```java long sizeInBytes = ConvertUtils.toByte("1GB"); String readableSize = ConvertUtils.byteToReadable(sizeInBytes); System.out.println(readableSize); //expected output: 1.00 GB ``` 希望这个工具类能够帮到您。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值