NumberUtil

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);

	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值