栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

数学计算工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数学计算工具类

小数转换成百分比时,保留两位小数

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.text.NumberFormat;


public class NumberUtils {


	
	public static Double percentage(Integer numerator, Integer denominator) {
		if (numerator == 0 || denominator == 0) {
			return 0.0;
		}
		double result = BigDecimal.valueOf(numerator).divide(BigDecimal.valueOf(denominator), 4, BigDecimal.ROUND_DOWN).doublevalue();
		return result;
	}
	
	
	public static String calPercentage(Integer numerator, Integer denominator) {
		if (numerator == 0 || denominator == 0) {
			return "0.00";
		}
		double result = BigDecimal.valueOf(numerator).divide(BigDecimal.valueOf(denominator), 4, BigDecimal.ROUND_DOWN)
				.multiply(BigDecimal.valueOf(100)).doublevalue();
		return roundByScale(result, 2);
	}

	
	public static String calPercentageHalfUp(Integer numerator, Integer denominator) {
		if (numerator == null || denominator == null) {
			return null;
		}
		if (numerator == 0 || denominator == 0) {
			return "0.00";
		}
		double result = BigDecimal.valueOf(numerator).divide(BigDecimal.valueOf(denominator), 4, BigDecimal.ROUND_HALF_UP)
				.multiply(BigDecimal.valueOf(100)).doublevalue();
		return roundByScale(result, 2);
	}
	
	
	public static String calDivideHalfUp(Integer numerator, Integer denominator) {
		if (numerator == 0 || denominator == 0) {
			return "0.00";
		}
		double result = BigDecimal.valueOf(numerator).divide(BigDecimal.valueOf(denominator), 2, BigDecimal.ROUND_HALF_UP).doublevalue();
		return roundByScale(result, 2);
	}

	
	// object --> BigDecimal
	public static BigDecimal getBigDecimal(Object value) {
		BigDecimal ret = null;
		if (value != null) {
			if (value instanceof BigDecimal) {
				ret = (BigDecimal) value;
			} else if (value instanceof String) {
				ret = new BigDecimal((String) value);
			} else if (value instanceof BigInteger) {
				ret = new BigDecimal((BigInteger) value);
			} else if (value instanceof Number) {
				ret = new BigDecimal(((Number) value).doublevalue());
			} else {
				throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass()
						+ " into a BigDecimal.");
			}
		}
		return ret;
	}

	public static Integer parseInt(String num) {
		Integer result = 0;
		if (StringUtils.isNotEmpty(num)) {
			try {
				result = Integer.parseInt(num);
			} catch (NumberFormatException e) {
				result = 0;
			}
		}
		return result;
	}
	
	public static Integer parseInt(String str, Integer defaultValue) {
		try {
			return Integer.parseInt(str);
		} catch (NumberFormatException e) {
			return defaultValue;
		}
	}

	
	public static String calPercentage(Object value) {
		if (null != value) {
			BigDecimal decimal = getBigDecimal(value);
			if (decimal.compareTo(BigDecimal.ZERO) == 0) {
				return "0.00";
			}
			decimal = decimal.setScale(4, BigDecimal.ROUND_HALF_UP);
			NumberFormat percent = NumberFormat.getPercentInstance();
			percent.setMaximumFractionDigits(4);
			String format = percent.format(decimal.doublevalue()).replace(",", "").split("%")[0];
			return roundByScale(Double.parseDouble(format), 2);
		}
		return null;
	}

	
	public static String calPercentage(BigDecimal value) {
		if (null != value) {
			if (value.compareTo(BigDecimal.ZERO) == 0) {
				return "0.00";
			}
			value = value.setScale(4, BigDecimal.ROUND_HALF_UP);
			NumberFormat percent = NumberFormat.getPercentInstance();
			percent.setMaximumFractionDigits(4);
			String format = percent.format(value.doublevalue()).replace(",", "").split("%")[0];
			return roundByScale(Double.parseDouble(format), 2);
		}
		return null;
	}

	
	public static String formatToPercent(BigDecimal value) {
		// 2位小数
		DecimalFormat df = new DecimalFormat("0.00"); 
		if (value == null) {
			return df.format(0);
		}
		return df.format(value.doublevalue());
	}

	public static void main(String[] args) {
		Integer a = 0;
		Integer b = 10;
		Double d = percentage(a, b);
		System.out.println(d);
		System.out.println(roundByScale(30.305, 2));// 30.30 roundByScale方法并非四舍五入,处理中double转为bigdecimal的时候会失去精度
		System.out.println();

		System.out.println("===================================");
		a = 3;
		b = 9;
		BigDecimal c = BigDecimal.valueOf(a).divide(BigDecimal.valueOf(b), 4, BigDecimal.ROUND_HALF_UP);
		String recordEffectiveRate = NumberUtils.calPercentage(c);
		System.out.println(recordEffectiveRate);

	}

	
	public static String roundByScale(double v, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("The   scale   must   be   a   positive   integer   or   zero");
		}
		if (scale == 0) {
			return new DecimalFormat("0").format(v);
		}
		String formatStr = "0.";
		for (int i = 0; i < scale; i++) {
			formatStr = formatStr + "0";
		}
		return new DecimalFormat(formatStr).format(v);
	}

	
	public static BigDecimal divide(Integer numerator, Integer denominator) {
		if (numerator == null || denominator == null || numerator == 0 || denominator == 0) {
			return BigDecimal.valueOf(0.00);
		}
		return BigDecimal.valueOf(numerator).divide(BigDecimal.valueOf(denominator), 4, BigDecimal.ROUND_DOWN);
	}

	public static Double BigDecimal2Double(BigDecimal b) {
		if (b == null) {
			return null;
		}
		return b.doublevalue();
	}

	public static Double string2Double(String s, Double whenError) {
		try {
			return Double.parseDouble(s);
		} catch (Exception e) {
			return whenError;
		}
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/777701.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号