Commit d6f7f695 by wuchengwu

同比环比工具类

parent 4c4396f8
......@@ -12,8 +12,8 @@ public class NumberUtil {
/**
* 获取a所占b的百分比
* @param a
* @param b
* @param a 分子
* @param b 分母
* @return
*/
public static String getPercent(int a, int b){
......@@ -27,4 +27,37 @@ public class NumberUtil {
nt.setMinimumFractionDigits(2);
return nt.format(percent);
}
/**
* 同比环比增长或下降
* @param a 分子
* @param b 分母
* @return
*/
public static String getTbhb(int a, int b){
if(b==0){
return "0.00%";
}
double percent = (double) a/ (double) b;
//正负控制 true + false -
boolean falg = true;
if (percent < 1){
percent = 1-percent;
falg = false;
}else if (percent > 1) {
percent = percent - 1;
}
//获取格式化对象
NumberFormat nt = NumberFormat.getPercentInstance();
//设置百分数精确度2即保留两位小数
nt.setMinimumFractionDigits(2);
String format = nt.format(percent);
if (falg){
return "上升"+format;
}else {
return "下降"+format;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment