Commit b2f16228 authored by yanzg's avatar yanzg

升级新版本

parent 692c1d8c
......@@ -5,9 +5,11 @@ import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.exception.ExceptionHelper;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -444,7 +446,16 @@ public class StringHelper {
}
}
private static boolean isBaseType(String clsName, Class<?> cls, Class<?> fromCls, Object fromValue) {
/**
* 是否属于基础类型
*
* @param clsName 基础类型名称
* @param cls 基础类型
* @param fromCls 来源类型
* @param fromValue 基础类型值
* @return 是否属于基础类型
*/
public static boolean isBaseType(String clsName, Class<?> cls, Class<?> fromCls, Object fromValue) {
if (clsName.equals(fromCls.getName())) {
return true;
}
......@@ -461,9 +472,34 @@ public class StringHelper {
* @return 转换成功的字符串
*/
public static String toString(Object obj) {
Class<?> fromCls = obj != null ? obj.getClass() : Object.class;
boolean isDouble = StringHelper.isBaseType(StringHelper.TYPE_DOUBLE, Double.class, fromCls, obj);
boolean isFromDouble = obj instanceof BigDecimal || isDouble;
if (isFromDouble) {
// 格式化设置(显示所有整数部分,小数点保留2位)
DecimalFormat decimalFormat = new DecimalFormat("#.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
String to = decimalFormat.format(obj);
String[] split = to.split("\\.");
StringBuilder sb = new StringBuilder(split[0]);
if (sb.length() == 0) {
sb.append("0");
}
if (split.length > 1) {
// 去掉右边的0
String right = StringHelper.trimRight(split[1], "0");
if (!right.isEmpty()) {
sb.append(".");
sb.append(right);
}
}
return sb.toString();
} else {
return obj == null ? null : obj.toString();
}
}
/**
* 将字符串转换为 bool ,当不能转换时,则默认为false
*
......
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