Commit 12e8c6a1 authored by yanzg's avatar yanzg

升级新版本

parent 6c26afda
...@@ -5,9 +5,11 @@ import com.yanzuoguang.util.base.ObjectHelper; ...@@ -5,9 +5,11 @@ import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.exception.ExceptionHelper; import com.yanzuoguang.util.exception.ExceptionHelper;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
...@@ -444,7 +446,16 @@ public class StringHelper { ...@@ -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())) { if (clsName.equals(fromCls.getName())) {
return true; return true;
} }
...@@ -461,7 +472,32 @@ public class StringHelper { ...@@ -461,7 +472,32 @@ public class StringHelper {
* @return 转换成功的字符串 * @return 转换成功的字符串
*/ */
public static String toString(Object obj) { public static String toString(Object obj) {
return obj == null ? null : obj.toString(); 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();
}
} }
/** /**
......
...@@ -4,6 +4,8 @@ import com.yanzuoguang.util.helper.StringHelper; ...@@ -4,6 +4,8 @@ import com.yanzuoguang.util.helper.StringHelper;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.math.BigDecimal;
public class TestStringHelper { public class TestStringHelper {
@Test @Test
public void testFirstRun() { public void testFirstRun() {
...@@ -38,5 +40,25 @@ public class TestStringHelper { ...@@ -38,5 +40,25 @@ public class TestStringHelper {
System.out.println(StringHelper.trim("1005050103", "1", "0", "3")); System.out.println(StringHelper.trim("1005050103", "1", "0", "3"));
} }
@Test
public void testToString() {
System.out.println(StringHelper.toString(0.0000000));
System.out.println(StringHelper.toString(50.0000001));
System.out.println(StringHelper.toString(0.02));
System.out.println(StringHelper.toString(0.0000001));
System.out.println(StringHelper.toString(50.00050000));
System.out.println(StringHelper.toString(0.00050000));
System.out.println(StringHelper.toString(0.1000000));
System.out.println(StringHelper.toString(5.1000000));
System.out.println(StringHelper.toString(100.1000000));
System.out.println(StringHelper.toString(new BigDecimal("0.0000000")));
System.out.println(StringHelper.toString(new BigDecimal("0.0000001")));
System.out.println(StringHelper.toString(new BigDecimal("0.00050000")));
System.out.println(StringHelper.toString(new BigDecimal("0.1000000")));
System.out.println(StringHelper.toString(new BigDecimal("100.1000000")));
}
} }
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