小数转换成百分比时,保留两位小数
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;
}
}
}



