2023-10-08 18:44:32 +08:00
|
|
|
|
package com.zhgd.xmgl.util;
|
|
|
|
|
|
|
2024-03-15 18:46:24 +08:00
|
|
|
|
import cn.hutool.core.convert.Convert;
|
2023-10-08 18:44:32 +08:00
|
|
|
|
import cn.hutool.core.util.NumberUtil;
|
2024-03-15 18:46:24 +08:00
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
2024-04-14 21:05:01 +08:00
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2023-10-08 18:44:32 +08:00
|
|
|
|
|
2024-04-07 16:20:41 +08:00
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
2024-04-14 21:05:01 +08:00
|
|
|
|
@Slf4j
|
2023-10-08 18:44:32 +08:00
|
|
|
|
public class NumberUtils {
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 除以
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param b1
|
|
|
|
|
|
* @param b2
|
|
|
|
|
|
* @param scale
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
2023-12-14 14:15:14 +08:00
|
|
|
|
public static Double div(Double b1, Double b2, Integer scale) {
|
2023-10-08 18:44:32 +08:00
|
|
|
|
if (b1 == null || b2 == null || b2.equals(0D)) {
|
|
|
|
|
|
return 0D;
|
|
|
|
|
|
}
|
|
|
|
|
|
return NumberUtil.round(b1 / b2, scale).doubleValue();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-23 10:45:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 涨跌幅比率,b1比b2增长多少
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param b1
|
|
|
|
|
|
* @param b2
|
|
|
|
|
|
* @param scale 保留小数位
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static Double rate(Double b1, Double b2, Integer scale) {
|
|
|
|
|
|
if (b1 == null || b2 == null || b2.equals(0D)) {
|
|
|
|
|
|
return 0D;
|
|
|
|
|
|
}
|
|
|
|
|
|
return NumberUtil.round((b1 - b2) / b2 * 100, scale).doubleValue();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-14 14:15:14 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 大于等于
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static boolean ge(Double b1, Double b2) {
|
|
|
|
|
|
return b1 != null && b2 != null && b1 >= b2;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-15 18:46:24 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 大于
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static boolean gt(String s1, String s2) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (!NumberUtil.isNumber(s1) || !NumberUtil.isNumber(s2)) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2024-03-16 11:58:15 +08:00
|
|
|
|
return Convert.toDouble(s1) > Convert.toDouble(s2);
|
2024-03-15 18:46:24 +08:00
|
|
|
|
} catch (Exception e) {
|
2024-04-14 21:05:01 +08:00
|
|
|
|
log.error("error:", e);
|
2024-03-15 18:46:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-14 14:15:14 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 小于等于
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static boolean le(Double b1, Double b2) {
|
|
|
|
|
|
return b1 != null && b2 != null && b1 <= b2;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-27 17:45:53 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* description: 使用 String.format 格式化数字,实现左侧补 0
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param num 需要格式化的数字
|
|
|
|
|
|
* @param digit 生成字符串长度(保留数字位数)
|
|
|
|
|
|
* @return String
|
|
|
|
|
|
* @version v1.0
|
|
|
|
|
|
* @author w
|
|
|
|
|
|
* @date 2019年7月19日 下午2:14:31
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String fillZero(int num, int digit) {
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 0:表示前面补0
|
|
|
|
|
|
* digit:表示保留数字位数
|
|
|
|
|
|
* d:表示参数为正数类型
|
|
|
|
|
|
*/
|
|
|
|
|
|
return String.format("%0" + digit + "d", num);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-06 16:57:17 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 相乘
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static Double multiply(Double b1, Double b2) {
|
|
|
|
|
|
if (b1 != null && b2 != null) {
|
|
|
|
|
|
return b1 * b2;
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-15 18:46:24 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取两个字符串最大的
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param d1
|
|
|
|
|
|
* @param d2
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String getMaxStr(String d1, String d2) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (StrUtil.isBlank(d1) && StrUtil.isBlank(d2)) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (StrUtil.isBlank(d1)) {
|
|
|
|
|
|
return d2;
|
|
|
|
|
|
} else if (StrUtil.isBlank(d2)) {
|
|
|
|
|
|
return d1;
|
|
|
|
|
|
}
|
|
|
|
|
|
return Convert.toStr(Math.max(Double.parseDouble(d1), Double.parseDouble(d2)));
|
|
|
|
|
|
} catch (NumberFormatException e) {
|
2024-04-14 21:05:01 +08:00
|
|
|
|
log.error("error:", e);
|
2024-03-15 18:46:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2024-04-07 16:20:41 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 生成数字随机数
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param place 定义随机数的位数
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String randomNum(int place) {
|
|
|
|
|
|
String base = "0123456789";
|
|
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
|
|
Random rd = new Random();
|
|
|
|
|
|
for (int i = 0; i < place; i++) {
|
|
|
|
|
|
sb.append(base.charAt(rd.nextInt(base.length())));
|
|
|
|
|
|
}
|
|
|
|
|
|
return sb.toString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
System.out.println(randomNum(6));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-08 18:44:32 +08:00
|
|
|
|
}
|