200 lines
3.6 KiB
Plaintext
200 lines
3.6 KiB
Plaintext
|
|
package com.jysoft.weChat.util;
|
||
|
|
|
||
|
|
import org.springframework.util.StringUtils;
|
||
|
|
|
||
|
|
public class ParseUtil {
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 数字格式化
|
||
|
|
*
|
||
|
|
* @param o
|
||
|
|
* 支持Float/Double/Integer
|
||
|
|
* @param precision
|
||
|
|
* 有效数字
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
public static String parseText(Object o, int precision) {
|
||
|
|
return parseText(o, precision, "");
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 数字格式化
|
||
|
|
*
|
||
|
|
* @param o
|
||
|
|
* 支持Float/Double/Integer
|
||
|
|
* @param precision
|
||
|
|
* 有效数字
|
||
|
|
* @param defaultValue
|
||
|
|
* 如果为空,则返回此值
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
public static String parseText(Object o, int precision, String defaultValue) {
|
||
|
|
if (o != null) {
|
||
|
|
if (precision < 0) {
|
||
|
|
return o.toString();
|
||
|
|
}
|
||
|
|
if (o instanceof Float || o instanceof Double) {
|
||
|
|
return String.format("%." + String.valueOf(precision) + "f", o);
|
||
|
|
} else if (o instanceof Integer) {
|
||
|
|
return String.format("%d", o);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return defaultValue;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Object乘以某只值转Float
|
||
|
|
*
|
||
|
|
* @param o
|
||
|
|
* @param multi
|
||
|
|
* 乘以
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
public static Float parseFloat(Object o, float multi) {
|
||
|
|
return parseFloat(o, multi, null);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Object乘以某只值转Float
|
||
|
|
*
|
||
|
|
* @param o
|
||
|
|
* @param multi
|
||
|
|
* 乘以
|
||
|
|
* @param defaultValue
|
||
|
|
* 如果为空,则返回此值
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
public static Float parseFloat(Object o, float multi, Float defaultValue) {
|
||
|
|
Float f = parseFloat(o);
|
||
|
|
if (f != null) {
|
||
|
|
return f * multi;
|
||
|
|
}
|
||
|
|
return defaultValue;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Object乘以某值转Double
|
||
|
|
*/
|
||
|
|
public static Double parseDouble(Object o, float multi) {
|
||
|
|
return parseDouble(o, multi, null);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Object乘以某值转Double
|
||
|
|
*/
|
||
|
|
public static Double parseDouble(Object o, float multi, Double defaultValue) {
|
||
|
|
Double d = parseDouble(o);
|
||
|
|
if (d != null) {
|
||
|
|
return d * multi;
|
||
|
|
}
|
||
|
|
return defaultValue;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 转换为字符串
|
||
|
|
*/
|
||
|
|
public static String parseText(Object part) {
|
||
|
|
if (part == null) {
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
return part.toString();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 转换为字符串
|
||
|
|
*/
|
||
|
|
public static String parseText(String part) {
|
||
|
|
if (part == null) {
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
return part.trim();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 转换为整型
|
||
|
|
*/
|
||
|
|
public static Integer parseInteger(Object part) {
|
||
|
|
try {
|
||
|
|
if (part instanceof Integer) {
|
||
|
|
return (Integer) part;
|
||
|
|
}
|
||
|
|
String s = parseText(part);
|
||
|
|
if (StringUtils.hasText(s)) {
|
||
|
|
return Integer.parseInt(s);
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static Integer parseInteger(Object part,int def) {
|
||
|
|
try {
|
||
|
|
if (part instanceof Integer) {
|
||
|
|
return (Integer) part;
|
||
|
|
}
|
||
|
|
String s = parseText(part);
|
||
|
|
if (StringUtils.hasText(s)) {
|
||
|
|
return Integer.parseInt(s);
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
|
||
|
|
}
|
||
|
|
return def;
|
||
|
|
}
|
||
|
|
/**
|
||
|
|
* 转换为Long
|
||
|
|
*/
|
||
|
|
public static Long parseLong(Object part) {
|
||
|
|
try {
|
||
|
|
if (part instanceof Long) {
|
||
|
|
return (Long) part;
|
||
|
|
}
|
||
|
|
String s = parseText(part);
|
||
|
|
if (StringUtils.hasText(s)) {
|
||
|
|
return Long.parseLong(s);
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 转换为Float
|
||
|
|
*/
|
||
|
|
public static Float parseFloat(Object part) {
|
||
|
|
try {
|
||
|
|
if (part instanceof Float) {
|
||
|
|
return (Float) part;
|
||
|
|
}
|
||
|
|
String s = parseText(part);
|
||
|
|
if (StringUtils.hasText(s)) {
|
||
|
|
return Float.parseFloat(s);
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 转换为Double
|
||
|
|
*/
|
||
|
|
public static Double parseDouble(Object part) {
|
||
|
|
try {
|
||
|
|
if (part instanceof Double) {
|
||
|
|
return (Double) part;
|
||
|
|
}
|
||
|
|
String s = parseText(part);
|
||
|
|
if (StringUtils.hasText(s)) {
|
||
|
|
return Double.parseDouble(s);
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|