All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.zcj.util.UtilMath Maven / Gradle / Ivy
package com.zcj.util;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class UtilMath {
/*
* 计算总额
*/
public static BigDecimal sumValue(BigDecimal... values) {
BigDecimal result = new BigDecimal("0.00");
if (values != null) {
for (BigDecimal value : values) {
if (value != null) {
result = result.add(value);
}
}
}
return result;
}
/**
* BigDecimal 转 String,保留小数点后两位
*
* @param val BigDecimal 的值
* @return 四舍五入;不使用科学计数法;小数点后显示固定位数,不足则用 0 补齐;如果参数为 null,则返回空字符串
*/
public static String bigDecimalToString(BigDecimal val) {
return bigDecimalToString(val, 2);
}
/**
* BigDecimal 转 String
*
* @param val BigDecimal 的值
* @param scale 保留小数点后几位
* @return 四舍五入;不使用科学计数法;小数点后显示固定位数(默认 2 位),不足则用 0 补齐;如果参数为 null,则返回空字符串
*/
public static String bigDecimalToString(BigDecimal val, Integer scale) {
if (val == null) {
return "";
}
if (scale == null) {
scale = 2;
}
return val.setScale(scale, BigDecimal.ROUND_HALF_UP).toPlainString();
}
/*
* 计算折扣
*/
public static double discount(Float theOld, Float theNew) {
return Math.ceil((theNew / theOld) * 100) / 10;
}
/*
* 计算百分比
*/
public static String percent(int value, int full) {
if (full == 0 || value < 0 || full <= 0) {
return null;
}
return percent(Double.valueOf(value), Double.valueOf(full));
}
/*
* 计算百分比
*/
public static String percent(Long value, Long full) {
if (full == 0 || value < 0 || full <= 0) {
return null;
}
return percent(Double.valueOf(value), Double.valueOf(full));
}
public static BigDecimal percentNumber(final Long value, final Long full) {
if (full == 0 || value < 0 || full <= 0) {
return null;
}
return percentNumber(Double.valueOf(value), Double.valueOf(full));
}
public static BigDecimal percentNumber(final Double value, final Double full) {
if (value == null || full == null) {
return null;
}
double percent = 0;
if (value > 0 && full > 0) {
percent = value / full * 100;
}
BigDecimal bd = new BigDecimal(String.valueOf(percent));
return bd.setScale(2, BigDecimal.ROUND_HALF_UP);
}
/*
* 计算百分比
*/
public static String percent(Double value, Double full) {
if (value == null || full == null) {
return null;
}
double percent = 0;
if (value > 0 && full > 0) {
percent = value / full;
}
NumberFormat nt = NumberFormat.getPercentInstance();
nt.setMinimumFractionDigits(2);
return nt.format(percent);
}
/*
* 计算平均值
*/
public static Double avgValue(Double[] array) {
if (array == null || array.length == 0) {
return null;
}
Double sum = sumValue(array);
if (sum == null) {
return null;
}
return sum / array.length;
}
/*
* 计算总分
*/
public static Double sumValue(Double[] array) {
if (array == null || array.length == 0) {
return null;
}
double sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
return sum;
}
/*
* 计算极大值
*/
public static > T maxValue(T[] array) {
if (array == null || array.length == 0) {
return null;
}
Arrays.sort(array);
return array[array.length - 1];
}
/*
* 计算极小值
*/
public static > T minValue(T[] array) {
if (array == null || array.length == 0) {
return null;
}
Arrays.sort(array);
return array[0];
}
/*
* 计算中值
*/
public static Double midValue(Double[] array) {
if (array == null || array.length == 0) {
return null;
}
List list = Arrays.asList(array);
Collections.sort(list);
if (list.size() % 2 == 1) {
return list.get((list.size() - 1) / 2);
} else {
return avgValue(new Double[]{list.get(list.size() / 2), list.get((list.size() / 2) - 1)});
}
}
/*
* 计算方差
*/
public static Double variance(Double[] values) {
if (values == null || values.length == 0) {
return null;
}
Double avg = avgValue(values);
if (avg == null) {
return null;
}
double sum = 0D;
for (int i = 0; i < values.length; i++) {
sum += (values[i] - avg) * (values[i] - avg);
}
sum /= values.length;
return sum;
}
/*
* 计算标准差
*/
public static Double standardDeviation(Double[] values) {
if (values == null || values.length == 0) {
return null;
}
Double variance = variance(values);
if (variance == null) {
return null;
}
return Math.sqrt(variance);
}
/*
* 计算标准分[原始分数-所有数据的平均分]/所有数据的标准差
*/
public static Double[] standardScore(Double[] all, Double[] myValues) {
if (all == null || all.length == 0 || myValues == null || myValues.length == 0 || all.length < myValues.length) {
return null;
}
Double[] s = new Double[myValues.length];
Double scoreMean = avgValue(all);
if (scoreMean == null) {
return null;
}
Double standardDeviation = standardDeviation(all);
if (standardDeviation == null || standardDeviation == 0) {
return null;
}
for (int i = 0; i < myValues.length; i++) {
s[i] = (myValues[i] - scoreMean) / standardDeviation;
}
return s;
}
/*
* 计算T总分均值(初中:70+15*标准分;小学:80+10*标准分)
*/
public static Double tAvgValue(Double[] all, Double[] myValues, String type) {
if (all == null || all.length == 0 || myValues == null || myValues.length == 0 || all.length < myValues.length) {
return null;
}
Double[] s = standardScore(all, myValues);
if (s == null) {
return null;
}
Double avg = avgValue(s);
if (avg == null) {
return null;
}
if ("初中".equals(type)) {
return 15 * avg + 70D;
} else if ("小学".equals(type)) {
return 10 * avg + 80D;
} else {
return null;
}
}
/*
* 计算难度值
*/
public static Double difficultyValue(Double avg, Double full) {
if (avg == null || full == null || full.floatValue() == 0) {
return null;
}
return avg / full;
}
/*
* 计算满分数
*/
public static int fullCount(Double[] arrays, Double full) {
int result = 0;
if (arrays != null && arrays.length > 0 && full != null && full.floatValue() > 0) {
for (Double s : arrays) {
if (s.floatValue() == full.floatValue()) {
result++;
}
}
}
return result;
}
/*
* 计算零分数
*/
public static int zeroCount(Double[] arrays) {
int result = 0;
if (arrays != null && arrays.length > 0) {
for (Double s : arrays) {
if (s.floatValue() == 0) {
result++;
}
}
}
return result;
}
/*
* 计算区分度: [27%高分组的平均分-27%低分组的平均分]÷满分值
*/
public static Double discrimination(Double[] values, Double full) {
if (values == null || values.length == 0 || full == null || full <= 0) {
return null;
}
List r = Arrays.asList(values);
Collections.sort(r);
Double a = avgValue(UtilCollection.getSubDoubleArray(r, 0, (int) (r.size() * 0.27)));
Double b = avgValue(UtilCollection.getSubDoubleArray(r, (int) (r.size() * 0.73), r.size()));
if (a == null || b == null) {
return null;
}
return (b - a) / full;
}
// 计算弧长
private static double rad(double d) {
return d * Math.PI / 180.0;
}
/**
* 计算两个坐标的距离,单位米
*
* @param lng1 第一点经度
* @param lat1 第一点纬度
* @param lng2 第二点经度
* @param lat2 第二点纬度
* @return 距离,单位米
*/
public static double pointShortestDistance(double lng1, double lat1, double lng2, double lat2) {
double earth_radius = 6378.137;// 地球半径
double radLat1 = rad(lat1);
double radLat2 = rad(lat2);
double a = radLat1 - radLat2;
double b = rad(lng1) - rad(lng2);
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2)
* Math.pow(Math.sin(b / 2), 2)));
s = s * earth_radius * 1000;
return s;
}
// 格式化距离
public static String distanceToStr(Double distance) {
if (distance == null) {
return null;
} else if (distance >= 1000) {
return new BigDecimal(String.valueOf(distance)).divide(new BigDecimal("1000"), 2, BigDecimal.ROUND_HALF_UP).toPlainString() + "km";
}
return new BigDecimal(String.valueOf(distance)).setScale(0, BigDecimal.ROUND_HALF_UP).toPlainString() + "m";
}
// 金额转大写
public static String numToChinese(String val) {
if (UtilString.isBlank(val)) {
return "";
}
String var1 = "零壹贰叁肆伍陆柒捌玖";
String var2 = "分角整圆拾佰仟万拾佰仟亿拾佰仟";
String var3 = val.trim();
String var4 = "";
try {
Float.parseFloat(var3);
} catch (Exception e) {
return "";
}
int var14;
if (var3.indexOf('.') == -1) {
var14 = var3.length();
} else {
var14 = var3.indexOf('.');
}
if (var14 > var2.length() - 3) {
return "";
}
String var9 = "";
String var10 = "";
for (int var12 = 0; var12 < var3.length() && var12 <= var14 + 2; ++var12) {
if (var12 != var14) {
int var7 = Integer.parseInt(String.valueOf(var3.charAt(var12)));
var9 = var1.substring(var7, var7 + 1);
var7 = var14 - var12 + 2;
var10 = var2.substring(var7, var7 + 1);
var4 = var4.concat(var9).concat(var10);
}
}
if (var14 == var3.length() || var14 == var3.length() - 1) {
var4 = var4.concat("整");
}
if (var14 == var3.length() - 2) {
var4 = var4.concat("零分");
}
while (var4.indexOf("零零") != -1) {
var4 = var4.replace("零零", "零");
}
var4 = var4.replace("零亿", "亿")
.replace("亿万", "亿")
.replace("零万", "万")
.replace("零仟", "零")
.replace("零佰", "零")
.replace("零拾", "零")
.replace("零元", "零")
.replace("零角", "")
.replace("零分", "");
return var4;
}
}