package com.whyc.util;
|
|
import io.swagger.models.auth.In;
|
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
|
/**
|
* 计算工具类
|
*/
|
public class MathUtil {
|
|
public static final Integer TYPE_FLOAT = 1;
|
public static final Integer TYPE_FLOAT_100 = 2;
|
public static final Integer TYPE_FLOAT_PERCENT = 3;
|
/**
|
* 提取公共方法,相除获取比例,返回可选String或者Float
|
* @param type 1表示小数,2表示除去%的比例,3表示%的比例
|
* @return Object String或者Float类型
|
* */
|
public static Object divide(Object num,Object num2,Integer type){
|
float res = 0;
|
double num2Double=Double.parseDouble(num2.toString());
|
if(num2Double!=0){
|
|
if (num instanceof Integer) {
|
res = BigDecimal.valueOf((Integer) num).divide(BigDecimal.valueOf((Integer) num2), 2, RoundingMode.HALF_UP).floatValue();
|
}
|
if (num instanceof Float) {
|
res = BigDecimal.valueOf((Float) num).divide(BigDecimal.valueOf((Float) num2), 2, RoundingMode.HALF_UP).floatValue();
|
}
|
if (num instanceof Double) {
|
res = BigDecimal.valueOf((Double) num).divide(BigDecimal.valueOf((Double) num2), 2, RoundingMode.HALF_UP).floatValue();
|
}
|
}
|
//0.05
|
if (type == 1) {
|
return res;
|
}
|
//5
|
else if (type == 2) {
|
return (int) (res * 100);
|
}
|
//5%
|
return (int) (res * 100) + "%";
|
}
|
|
/**
|
* 提取公共方法,相除获取比例,返回可选String或者Float
|
* @param type 1表示小数,2表示除去%的比例,3表示%的比例
|
* @return Object String或者Float类型
|
* */
|
public static Object divide(Object num, Object num2, Integer type, Integer scale){
|
Object res = 0;
|
double num2Double=Double.parseDouble(num2.toString());
|
double numDouble=Double.parseDouble(num.toString());
|
if(num2Double!=0){
|
//0.05
|
if (type == 1) {
|
res = BigDecimal.valueOf(numDouble).divide(BigDecimal.valueOf(num2Double), scale, RoundingMode.HALF_UP).floatValue();
|
}
|
//5
|
else {
|
BigDecimal multiply = BigDecimal.valueOf(numDouble).multiply(BigDecimal.valueOf(100));
|
BigDecimal divide = multiply.divide(BigDecimal.valueOf(num2Double), scale, RoundingMode.HALF_UP);
|
if (type == 2) {
|
res = divide.floatValue()+"";
|
}
|
//5%
|
else{
|
res = divide.floatValue()+"%";
|
}
|
}
|
return res;
|
}else{
|
return 0;
|
}
|
}
|
|
}
|