public class StrUtil extends cn.hutool.core.util.StrUtil{
private static final String UNIT = "万千佰拾亿千佰拾万千佰拾元角分";
private static final String DIGIT = "零壹贰叁肆伍陆柒捌玖";
private static final double MAX_VALUE = 9999999999999.99D;
private static final String NUM_CH[] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
/**
* 生成随机字符串
* @return
*/
public static String getRandomStr(int size) {
// 字符串
String string = "";
// 循环得到10个字母
for (int i = 0; i < size; i++) {
// 得到随机字母
char c = (char) ((Math.random() * 26) + 97);
// 拼接成字符串
string += (c + "");
}
return string;
}
}
/**
* 根据前缀生成与日期相关的随机号码串(前缀 + 16位),比如用于生成订单号和流水号等
* @param prefix 前缀可为空
* @return 前缀 + 16位字符串
*/
public static String getRandomNoWithDate(String prefix) {
int j = new Random().nextInt(10);
while (j == 0) {
j = new Random().nextInt(10);
}
if (prefix == null || "".equals(prefix)) {
return String.valueOf((int)((Math.random() + j)* 1000)) + System.nanoTime() + String.valueOf((int)((Math.random() + j)* 1000));
} else {
return prefix + String.valueOf((int)((Math.random() + j)* 1000)) + System.nanoTime() + String.valueOf((int)((Math.random() + j)* 1000));
}
}
/**
* 将String字符串转为set
* @param str 源字符串
* @param set 需要转成的set
*/
public static void string2set(String str, Set<String> set) {
if (StringUtils.isBlank(str)) {
return;
}
if (str.contains(",")) {
String[] strArr = str.split(",");
for (String s: strArr) {
set.add(s);
}
} else {
set.add(str);
}
}
/**
* 金额转大写
* @param v,以元为单位,两位小数,如 1.23
* @return
*/
public static String changeY2M(double v) {
if (v < 0 || v > MAX_VALUE){
return "参数非法!";
}
long l = Math.round(v * 100);
if (l == 0){
return "零元整";
}
String strValue = l + "";
// i用来控制数
int i = 0;
// j用来控制单位
int j = UNIT.length() - strValue.length();
String rs = "";
boolean isZero = false;
for (; i < strValue.length(); i++, j++) {
char ch = strValue.charAt(i);
if (ch == '0') {
isZero = true;
if (UNIT.charAt(j) == '亿' || UNIT.charAt(j) == '万' || UNIT.charAt(j) == '元') {
rs = rs + UNIT.charAt(j);
isZero = false;
}
} else {
if (isZero) {
rs = rs + "零";
isZero = false;
}
rs = rs + DIGIT.charAt(ch - '0') + UNIT.charAt(j);
}
}
if (!rs.endsWith("分")) {
rs = rs + "整";
}
rs = rs.replaceAll("亿万", "亿");
return rs;
}