具体的原理就不说了,这个连接里面都有
但是前辈其中有一行代码Func.parseBigDecimal(0.5)
,没有找到出处,导致就没法用,那没办法了,只能自己写了
上代码!!
package com.cloud.top.tower.common.utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author YanYan
* @description: 上中下四分卫数计算
* @date 2023/1/5 15:23
*/
public class FourDivSionUtil {
//list长度>=3
public static List<Double> calculate(List<Double> list) {
if (list.size() < 3) {
return Collections.emptyList();
}
list.sort(Double::compareTo);
//上四分卫数Uq
double Uq;
//中位数
double Median;
//下四分卫数Lq
double Lq;
int size = list.size();
if (size % 2 == 0) {
Median = (list.get(size / 2 - 1) + list.get(size / 2)) / 2;
double Q1 = (size + 1) / 4.0;
double Q2 = 3 * (size + 1) / 4.0;
int q1 = (int) Q1;
Uq = list.get(q1 - 1) + (list.get(q1) - list.get(q1 - 1)) * getDecimals(Q1);
int q2 = (int) Q2;
Lq = list.get(q2 - 1) + (list.get(q2) - list.get(q2 - 1)) * getDecimals(Q2);
} else {
double Q1 = (size + 1) * 0.25;
double Q2 = (size + 1) * 0.5;
double Q3 = (size + 1) * 0.75;
Median = list.get((int) (Q2 - 1));
if (isDecimals(Q1)) {
int q1 = (int) (Q1);
Uq = (list.get(q1 - 1) + list.get(q1)) * 0.5;
} else {
Uq = list.get((int) (Q1 - 1));
}
if (isDecimals(Q3)) {
int q3 = (int) (Q3);
Lq = (list.get(q3 - 1) + list.get(q3)) * 0.5;
} else {
Lq = list.get((int) (Q3 - 1));
}
}
List<Double> doubles = new ArrayList<>();
doubles.add(Uq);
doubles.add(Median);
doubles.add(Lq);
return doubles;
}
//判断num是否为小数
private static boolean isDecimals(double num) {
return (int) num != num;
}
//获取一个小数的小数部分
private static double getDecimals(double num) {
String numStr = String.valueOf(num);
String[] split = numStr.split("\\.");
if (split.length == 2) {
return Double.parseDouble(split[1]) * 0.01;
}
return 0;
}
}