|
|
@@ -0,0 +1,171 @@
|
|
|
+package com.sckw.core.utils;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.text.DecimalFormat;
|
|
|
+import java.text.NumberFormat;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author zk
|
|
|
+ * @description 数值处理类
|
|
|
+ * @date 2020/06/06 09:00:32
|
|
|
+ */
|
|
|
+public class NumberUtils {
|
|
|
+ private static final int SIX = 6;
|
|
|
+ public NumberUtils() {
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean parseBoolean(Object obj) {
|
|
|
+ if (obj == null) {
|
|
|
+ return false;
|
|
|
+ } else if (obj instanceof Boolean) {
|
|
|
+ return (Boolean)obj;
|
|
|
+ } else {
|
|
|
+ return obj instanceof String ? "true".equalsIgnoreCase(((String)obj).trim()) : false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int parseInt(Object obj) {
|
|
|
+ return parseInt(obj, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int parseInt(Object obj, int defaultValue) {
|
|
|
+ if (obj == null) {
|
|
|
+ return defaultValue;
|
|
|
+ } else if (obj instanceof String) {
|
|
|
+ try {
|
|
|
+ String str = ((String)obj).trim();
|
|
|
+ return str.length() == 0 ? defaultValue : Integer.parseInt(str);
|
|
|
+ } catch (NumberFormatException var3) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return obj instanceof Number ? ((Number)obj).intValue() : defaultValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static long parseLong(Object obj) {
|
|
|
+ return parseLong(obj, 0L);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static long parseLong(Object obj, long defaultValue) {
|
|
|
+ if (obj == null) {
|
|
|
+ return defaultValue;
|
|
|
+ } else if (obj instanceof String) {
|
|
|
+ try {
|
|
|
+ return Long.parseLong(((String)obj).trim());
|
|
|
+ } catch (NumberFormatException var4) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return obj instanceof Number ? ((Number)obj).longValue() : defaultValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static float parseFloat(Object obj) {
|
|
|
+ return parseFloat(obj, 0.0F);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static float parseFloat(Object obj, float defaultValue) {
|
|
|
+ if (obj == null) {
|
|
|
+ return defaultValue;
|
|
|
+ } else if (obj instanceof String) {
|
|
|
+ try {
|
|
|
+ return Float.parseFloat(((String)obj).trim());
|
|
|
+ } catch (NumberFormatException var3) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return obj instanceof Number ? ((Number)obj).floatValue() : defaultValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static double parseDouble(Object str) {
|
|
|
+ return parseDouble(str, 0.0D);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static double parseDouble(Object obj, double defaultValue) {
|
|
|
+ if (obj == null) {
|
|
|
+ return defaultValue;
|
|
|
+ } else if (obj instanceof String) {
|
|
|
+ try {
|
|
|
+ return Double.parseDouble(((String)obj).trim());
|
|
|
+ } catch (NumberFormatException var4) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return obj instanceof Number ? ((Number)obj).doubleValue() : defaultValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static BigDecimal parseBigDecimal(Object obj) {
|
|
|
+ return parseBigDecimal(obj, new BigDecimal(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static BigDecimal parseBigDecimal(Object obj, BigDecimal defaultValue) {
|
|
|
+ if (obj == null) {
|
|
|
+ return defaultValue;
|
|
|
+ } else if (obj instanceof String) {
|
|
|
+ try {
|
|
|
+ String s = (String)obj;
|
|
|
+ s = s.trim();
|
|
|
+ if (s.length() == 0) {
|
|
|
+ s = "0";
|
|
|
+ }
|
|
|
+
|
|
|
+ return new BigDecimal(s);
|
|
|
+ } catch (NumberFormatException var3) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ } else if (obj instanceof BigDecimal) {
|
|
|
+ return (BigDecimal)obj;
|
|
|
+ } else if (obj instanceof Integer) {
|
|
|
+ return new BigDecimal((Integer)obj);
|
|
|
+ } else if (obj instanceof Long) {
|
|
|
+ return new BigDecimal((Long)obj);
|
|
|
+ } else if (obj instanceof Float) {
|
|
|
+ return new BigDecimal((double)(Float)obj);
|
|
|
+ } else if (obj instanceof Double) {
|
|
|
+ return new BigDecimal((Double)obj);
|
|
|
+ } else {
|
|
|
+ return obj instanceof Number ? new BigDecimal(((Number)obj).doubleValue()) : defaultValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatNumberValue(Object obj) {
|
|
|
+ return formatNumber(obj, "###0.00");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatNumberStr(Object obj) {
|
|
|
+ return formatNumber(obj, "#,##0.00");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatNumberEmpty(Object obj) {
|
|
|
+ if (obj == null) {
|
|
|
+ obj = new BigDecimal(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ return ((Number)obj).intValue() == 0 ? "" : formatNumberStr(obj);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatNumber(Object obj, String pattern) {
|
|
|
+ if (obj == null) {
|
|
|
+ obj = new BigDecimal(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ NumberFormat nf = new DecimalFormat(pattern);
|
|
|
+ return nf.format(obj);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成六位随机数
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String createRandomVcode() {
|
|
|
+ //验证码
|
|
|
+ StringBuilder vcode = new StringBuilder();
|
|
|
+ for (int i = 0; i < SIX; i++) {
|
|
|
+ vcode.append((int) (Math.random() * 9));
|
|
|
+ }
|
|
|
+ return vcode.toString();
|
|
|
+ }
|
|
|
+}
|