实际开发中,经常会遇到单位转换、保留小数、计算百分比的场景,下面是我自己写的并实际使用的工具类,希望能帮到大家或者给大家参考
该工具类包括:
- 单位转换成万或亿
- 保留两位小数
- 计算百分比,保留两位小数
- 计算百分比,保留两位小数,若有效数字为0,则往后取到第一个不为零的
- 计算百分比,保留整数
public class NumberUtil {
/**
* 转换为亿
* @param amount
* @return
*/
public static BigDecimal formatToYI(BigDecimal amount) {
if (amount == null) {
return new BigDecimal(0);
}
return amount.divide(new BigDecimal(100000000), 2, BigDecimal.ROUND_HALF_UP);
}
/**
* 转换为万
* @param amount
* @return
*/
public static BigDecimal formatToWAN(BigDecimal amount) {
if (amount == null) {
return new BigDecimal(0);
}
return amount.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
}
/**
* 转成万或亿
* @param amount
* @return
*/
public static String formatToWanOrYi(BigDecimal amount) {
if (amount == null) {
return "0";
}
if (amount.compareTo(new BigDecimal(10000000)) < 0) {
//如果小于1亿
return amount.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString() + "万";
}
return amount.divide(new BigDecimal(100000000), 2, BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString() + "亿";
}
/**
* 保留两位小数
* @param amount
* @return
*/
public static BigDecimal formatTwo(BigDecimal amount) {
if (amount == null) {
return new BigDecimal(0);
}
return amount.setScale(2, BigDecimal.ROUND_HALF_UP);
}
/**
* 计算百分比 保留两位小数
* @param num1
* @param num2
* @return
*/
public static String getPercent(long num1, long num2) {
String rate = "0.00%";
String format = "0.00";
if (num1 != 0 && num2 != 0) {
DecimalFormat dec = new DecimalFormat(format);
rate = dec.format((double) num1 / num2*100) + "%";
} else if (num1 != 0 && num2 == 0) {
rate = "100.00%";
}
return rate;
}
/**
* 计算百分比 保留两位小数
* @param num1
* @param num2
* @return
*/
public static String getPercent(BigDecimal num1, BigDecimal num2) {
String rate = "0.00%";
String format = "0.00";
if (num1.intValue() != 0 && num2.intValue() != 0) {
DecimalFormat dec = new DecimalFormat(format);
rate = dec.format(num1.divide(num2, 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).doubleValue()) + "%";
} else if (num1.intValue() != 0 && num2.intValue() == 0) {
rate = "100.00%";
}
return rate;
}
/**
* 计算百分比,保留两位小数,若有效数字为0,则往后取到第一个不为零的
* @param num1
* @param num2
* @return
*/
public static String getPercentPro(long num1, long num2) {
String rate = "0.00%";
String format = "0.00";
if (num1 != 0 && num2 != 0) {
double v = (double) num1 / num2 * 100;
if (v >= 0.01) {
DecimalFormat dec = new DecimalFormat(format);
rate = dec.format(v) + "%";
} else {
while (true) {
DecimalFormat dec = new DecimalFormat(format);
String format1 = dec.format(v);
if (Double.parseDouble(format1) > 0) {
rate = format1 + "%";
break;
} else {
format = format + "0";
}
}
}
} else if (num1 != 0 && num2 == 0) {
rate = "100.00%";
}
return rate;
}
/**
* 计算百分比 保留整数
* @param num1
* @param num2
* @return
*/
public static String getPercentNoPoint(BigDecimal num1, BigDecimal num2) {
String rate = "0%";
String format = "0";
if (num1.intValue() != 0 && num2.intValue() != 0) {
DecimalFormat dec = new DecimalFormat(format);
rate = dec.format(num1.divide(num2, 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).doubleValue()) + "%";
} else if (num1.intValue() != 0 && num2.intValue() == 0) {
rate = "100%";
}
return rate;
}
标签:百分比,return,num1,num2,rate,amount,转换,小数,BigDecimal
From: https://www.cnblogs.com/GilbertDu/p/18245961