package cn.ding.util;
import java.math.BigDecimal;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
public final class NumberUtils {
final static int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, Integer.MAX_VALUE };
// Requires positive x
public static int stringSize(int x) {
for (int i = 0;; i++)
if (x <= sizeTable[i])
return i + 1;
}
/**
* 将Integer[]转换为int[]
*
* @param integerArray------欲转换的Integer[]
*
* @return 转换后的int[]
*/
public static int[] integerArrayToIntArray(Integer[] integerArray) {
int[] intArray = null;
if (integerArray != null && integerArray.length > 0) {
intArray = new int[integerArray.length];
for (int i = 0; i < integerArray.length; i++) {
intArray[i] = integerArray[i].intValue();
}
}
return intArray;
}
/**
* 将List<Integer>转换为int[]
*
* @param integerList------欲转换的List<Integer>
*
* @return 转换后的int[]
*/
public static int[] integerListToIntArray(List<Integer> integerList) {
int[] intArray = null;
if (integerList != null) {
intArray = integerArrayToIntArray((Integer[]) integerList.toArray(new Integer[0]));
}
return intArray;
}
/**
* String转换为BigDecimal
*
* @param str 字符串
* @return BigDecimal
*/
public static BigDecimal stringToBigDecimal(String str) {
if (StringUtils.isBlank(str)) {
return null;
}
String value = getEffectiveDigit(str);
return value == null ? null : new BigDecimal(value);
}
/**
* String转换为double
*
* @param str 字符串
* @return double
*/
public static double stringToDouble(String str) {
if (StringUtils.isBlank(str)) {
return 0.0d;
}
return NumberUtils.toDouble(getEffectiveDigit(str));
}
/**
* 获取有效数字
*
* @param str
* @return
*/
public static String getEffectiveDigit(String str) {
StringBuilder value = new StringBuilder();
// 32:" " ; 43:"+" ; 44:"," ; 45:"-" ; 46:"." ;48:"0" ; 57:"9"
for (int i = 0; i < str.length(); i++) {
// 去除千分位"," 与空格" "
if (str.charAt(i) == 44 || (str.charAt(i) == 32)) {
continue;
}
// 判断是否为正负型、小数点、数字
if ((value.length() == 0 && (str.charAt(i) == 43 || str.charAt(i) == 45)) || str.charAt(i) == 46
|| (str.charAt(i) >= 48 && str.charAt(i) <= 57)) {
value.append(str.charAt(i));
} else {
break;
}
}
if (value.length() == 1 && (value.charAt(0) == 43 || value.charAt(0) == 45 || value.charAt(0) == 46)) {
return null;
}
return value.toString();
}
/**
* 判断是否为负数
*
* @param str
* @return
*/
public static boolean isNegative(String str) {
if (StringUtils.isBlank(str)) {
return false;
}
return str.matches("-[0-9]+.*[0-9]*");
}
public static void main(String[] args) {
// 千分位","
BigDecimal decimal = stringToBigDecimal(" -20,134.54030 ");
System.out.println("decimal:" + decimal);
// 获取字符串中数字部分
BigDecimal bigDecimal = stringToBigDecimal(" -134.543sdfas3234 dd ");
System.out.println("bigDecimal:" + bigDecimal);
double d = stringToDouble("-1sdfas3234 dd");
System.out.println("double:" + d);
Boolean isNegative = isNegative("-10");
System.out.println("strResult:" + isNegative);
}
}
NumberUtil
最新推荐文章于 2024-10-03 08:21:43 发布