Java储存大小格式化工具

原创
2021/06/15 18:27
阅读数 262

工具类代码:


public abstract class StorageUnit {
	
	protected BigDecimal power;

	public static final StorageUnit B = new B();

	public static final StorageUnit KB = new KB();

	public static final StorageUnit MB = new MB();

	public static final StorageUnit GB = new GB();

	public static final StorageUnit TB = new TB();

	public static final StorageUnit PB = new PB();

	public static final StorageUnit EB = new EB();

	public static final StorageUnit ZB = new ZB();

	public static final StorageUnit YB = new YB();
	
	public static final StorageUnit BB = new BB();
	
	public static final StorageUnit NB = new NB();
	
	public static final StorageUnit DB = new DB();
	
	public static final StorageUnit CB = new CB();
	
	public static final StorageUnit XB = new XB();

	private static final BigDecimal binary = new BigDecimal(1024);

	/**
	 * 获取字节倍率
	 */
	abstract BigDecimal getPower();

	/**
	 * 大小转换
	 * 
	 * @param volume
	 * @param unit
	 * @return
	 */
	public BigDecimal format(BigDecimal volume, StorageUnit unit) {
		BigDecimal actualer = volume.multiply(unit.getPower()).divide(this.getPower(), 16, BigDecimal.ROUND_HALF_DOWN);
		return actualer.setScale(2, BigDecimal.ROUND_HALF_DOWN);
	}

	/**
	 * 大小转换
	 * 
	 * @param volume
	 * @param unit
	 * @return
	 */
	public BigDecimal format(Long volume, StorageUnit unit) {
		return format(new BigDecimal(volume), unit);
	}

	/**
	 * 大小转换
	 * 
	 * @param volume
	 * @param unit
	 * @return
	 */
	public BigDecimal format(Integer volume, StorageUnit unit) {
		return format(new BigDecimal(volume), unit);
	}

	/**
	 * 大小转换
	 * 
	 * @param volume
	 * @param unit
	 * @return
	 */
	public BigDecimal format(Double volume, StorageUnit unit) {
		return format(new BigDecimal(volume), unit);
	}

	public static class B extends StorageUnit {
		@Override
		BigDecimal getPower() {
			return BigDecimal.ONE;
		}
	}

	public static class KB extends StorageUnit {
		@Override
		BigDecimal getPower() {
			return binary;
		}
	}

	public static class MB extends StorageUnit {
		@Override
		BigDecimal getPower() {
			if (power == null) {
				power =binary.multiply(binary);
			}
			return power;
		}
	}

	public static class GB extends StorageUnit {
		@Override
		BigDecimal getPower() {
			if (power == null) {
				power =binary.multiply(binary).multiply(binary);
			}
			return power;
		}
	}

	public static class TB extends StorageUnit {
		@Override
		BigDecimal getPower() {
			if (power == null) {
				power =binary.multiply(binary).multiply(binary).multiply(binary);
			}
			return power;
		}
	}

	public static class PB extends StorageUnit {
		@Override
		BigDecimal getPower() {
			if (power == null) {
				power = binary.multiply(binary).multiply(binary).multiply(binary).multiply(binary);
			}
			return power;
		}
	}

	public static class EB extends StorageUnit {
		@Override
		BigDecimal getPower() {
			if (power == null) {
				power = binary.multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary);
			}
			return power;
		}
	}

	public static class ZB extends StorageUnit {
		@Override
		BigDecimal getPower() {
			if (power == null) {
				power = binary.multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary)
						.multiply(binary);
			}
			return power;
		}
	}

	public static class YB extends StorageUnit {
		@Override
		BigDecimal getPower() {
			if (power == null) {
				power = binary.multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary)
						.multiply(binary).multiply(binary);
			}
			return power;
		}
	}
	public static class BB extends StorageUnit {
		@Override
		BigDecimal getPower() {
			if (power == null) {
				power = binary.multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary)
						.multiply(binary).multiply(binary);
			}
			return power;
		}
	}
	public static class NB extends StorageUnit {
		@Override
		BigDecimal getPower() {
			if (power == null) {
				power = binary.multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary)
						.multiply(binary).multiply(binary);
			}
			return power;
		}
	}
	public static class DB extends StorageUnit {
		@Override
		BigDecimal getPower() {
			if (power == null) {
				power = binary.multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary)
						.multiply(binary).multiply(binary);
			}
			return power;
		}
	}
	public static class CB extends StorageUnit {
		@Override
		BigDecimal getPower() {
			if (power == null) {
				power = binary.multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary)
						.multiply(binary).multiply(binary);
			}
			return power;
		}
	}
	public static class XB extends StorageUnit {
		@Override
		BigDecimal getPower() {
			if (power == null) {
				power = binary.multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary).multiply(binary)
						.multiply(binary).multiply(binary);
			}
			return power;
		}
	}
	
}

使用方式:

	public static void main(String[] args) {
		System.out.println(StorageUnit.MB.format(1, StorageUnit.GB).toPlainString());//将1GB转为MB
	}

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部