数字工具类

public class NumberUtilz {

	/**
	 * 检查此字符串是否为数字
	 * 
	 * @param str
	 * @return true 如果是数字
	 */
	public static boolean isNumberic(String str) {

		if (str == null || str.equals("")) {
			return false;
		}

		for (int i = 0; i < str.length(); i++) {
			if (Character.isDigit(str.charAt(i)) == false) {
				return false;
			}
		}
		return true;
	}

	/**
	 * <p>
	 * 检查此字符串是否为正整数
	 * </p>
	 *
	 * <p>
	 * <code>null</code> will return <code>false</code>. An empty String ("") will
	 * return <code>false</code>.
	 * </p>
	 *
	 * <pre>
	 * StringUtils.isPositiveInteger(null)   = false
	 * StringUtils.isPositiveInteger("")     = false
	 * StringUtils.isPositiveInteger("  ")   = false
	 * StringUtils.isPositiveInteger("123")  = true
	 * StringUtils.isPositiveInteger("12 3") = false
	 * StringUtils.isPositiveInteger("ab2c") = false
	 * StringUtils.isPositiveInteger("12-3") = false
	 * StringUtils.isPositiveInteger("12.3") = false
	 * StringUtils.isPositiveInteger("00")   = false
	 * </pre>
	 *
	 * @param str 要检查的字符串,可能是null
	 * @return <code>true</code> 如果它是正整数的话
	 */
	public static boolean isPositiveInteger(String str) {

		return isNumberic(str) && !(str.startsWith("0") && str.length() > 1);
	}

	/**
	 * 检查此字符串是否为小数
	 * 
	 * @param num
	 * @return <code>true</code> 如果它是小数的话
	 */
	public static boolean isDecimal(String num) {

		if (num == null || (num.indexOf('.') == -1) || (num.split("\\.").length != 2)) {
			return false;
		}
		String[] str = num.split("\\.");
		if (!isPositiveInteger(str[0]) || !isNumberic(str[1])) {
			return false;
		}
		return true;
	}

	/**
	 * 去除数组中重复的元素
	 * 
	 * @param array int数组
	 * @return 无重复的int数组
	 */
	public int[] removeDuplicateNumber(int[] array) {
		List list = new ArrayList();
		for (int i = 0; i < array.length; i++) {
			if (!list.contains(new Integer(array[i]))) {
				list.add(new Integer(array[i]));
			}
		}
		int[] temp = new int[list.size()];
		for (int i = 0; i < list.size(); i++) {
			temp[i] = ((Integer) list.get(i)).intValue();
		}
		return temp;
	}

	/**
	 * 冒泡排序
	 * 
	 * @param x int数组
	 */
	public static int[] BubbleSort(int[] x) {
		for (int i = 0; i < x.length; i++) {
			for (int j = i + 1; j < x.length; j++) {
				if (x[i] > x[j]) {
					int temp = x[i];
					x[i] = x[j];
					x[j] = temp;
				}
			}
		}
		return x;
	}

	/**
	 * 选择排序
	 * 
	 * @param x int数组
	 */
	public static int[] selectSort(int[] x) {
		for (int i = 0; i < x.length; i++) {
			int lowerIndex = i;
			// 找出最小的一个索引
			for (int j = i + 1; j < x.length; j++) {
				if (x[j] < x[lowerIndex]) {
					lowerIndex = j;
				}
			}
			// 交换
			int temp = x[i];
			x[i] = x[lowerIndex];
			x[lowerIndex] = temp;
		}
		return x;
	}

	/**
	 * 插入排序
	 * 
	 * @param x int数组
	 */
	public static int[] insertionSort(int[] x) {
		for (int i = 1; i < x.length; i++) {// i从一开始,因为第一个数已经是排好序的啦
			for (int j = i; j > 0; j--) {
				if (x[j] < x[j - 1]) {
					int temp = x[j];
					x[j] = x[j - 1];
					x[j - 1] = temp;
				}
			}
		}
		return x;
	}

	/**
	 * 希尔排序
	 * 
	 * @param x int数组
	 */
	public static int[] shellSort(int[] x) {
		// 分组
		for (int increment = x.length / 2; increment > 0; increment /= 2) {
			// 每个组内排序
			for (int i = increment; i < x.length; i++) {
				int temp = x[i];
				int j = 0;
				for (j = i; j >= increment; j -= increment) {
					if (temp < x[j - increment]) {
						x[j] = x[j - increment];
					} else {
						break;
					}
				}
				x[j] = temp;
			}
		}
		return x;
	}

	/**
	 * 得到6为随机数
	 */
	public static int getSixFigures() {
		int i = new Random().nextInt(999999);
		return i < 100000 ? i + 100000 : i;
	}

	/**
	 * 10进制int转换为16进制String
	 * 
	 * @param dec 10进制数据
	 * @return String
	 */
	public static String dec2Hex(int dec) {
		StringBuffer sb = new StringBuffer();
		sb.append("0x");

		for (int i = 0; i < 8; i++) {
			int tmp = (dec >> (7 - i % 8) * 4) & 0x0f;

			if (tmp < 10)
				sb.append(tmp);
			else
				sb.append((char) ('A' + (tmp - 10)));
		}

		return sb.toString();
	}

	/**
	 * 16进制String转换为10进制int
	 * 
	 * @param hexStr 16进制数据
	 * @return int
	 */
	public static int hex2Dec(String hexStr) {
		String myStr[] = { "a", "b", "c", "d", "e", "f" };
		int result = 0;
		;
		int n = 1;
		for (int i = hexStr.length() - 1; i >= 0; i--) {
			String param = hexStr.substring(i, i + 1);
			for (int j = 0; j < myStr.length; j++) {
				if (param.equalsIgnoreCase(myStr[j])) {
					param = "1" + String.valueOf(j);

				}
			}
			result = Integer.parseInt(param) * n;
			n *= 16;
		}
		return result;
	}

	/**
	 * 判断是不是手机号码
	 * 
	 * @param number
	 * @return true-是手机号码 false-不是手机号
	 */
	public static boolean isPhoneNumber(String number) {
		Assert.notNull(number, "要检测的手机号不能为空");
		Pattern pattern = Pattern
				.compile("(^0{0,1}13[0-9]{9}$)|(13\\d{9}$)|(147\\d{8}$)|(15[01235-9]\\d{8}$)|(18[025-9]\\d{8}$)");
		Matcher matcher = pattern.matcher(number);
		if (matcher.find()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 得到i位随机数
	 * 
	 * @param num>0
	 * @return
	 */
	public static long getFigures(int num) {
		return (long) (Math.random() * 9 * Math.pow(10, num - 1)) + (long) Math.pow(10, num - 1);
	}

	/**
	 * 判断字符串是否为数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isIntOrDouble(String str) {
		Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/** * @project: WebProjectUtil * @class: NumberUtil * @describe: 此工具类用来处理数字方面的逻辑, * 如返回指定位数的随机数字、Double的加减乘除精确运算、指定位数数字用“0”补齐 * @autho: Administrator * @date: 2013-6-7 下午02:26:27 * @alter: Administrator * @alterDate: 2013-6-7 下午02:26:27 * @alterRemark: * @version V1.0 */ public class NumberUtil { private static final int DEF_DIV_SCALE = 2; /** * @return 返回12位随机数 */ public static String randomNumber() { } /** * @param parm * @return 返回指定位数随机数 */ public static String randomNumber(int parm) { } /** * * 两个Double数相加 * * @param v1 * @param v2 * @return Double */ public static Double add(Double v1, Double v2) { } /** * * 两个Double数相减 * * @param v1 * @param v2 * @return Double */ public static Double sub(Double v1, Double v2) { } /** * * 两个Double数相乘 * * @param v1 * @param v2 * @return Double */ public static Double mul(Double v1, Double v2) { } /** * * 两个Double数相除 * * @param v1 * @param v2 * @return Double */ public static Double div(Double v1, Double v2) { } /** * * 两个Double数相除,并保留scale位小数 * * @param v1 * @param v2 * @param scale * @return Double */ public static Double div(Double v1, Double v2, int scale) { } /** * 返回指定Double的负数 * @param v1 * @return */ public static Double neg(Double v1) { /** * @Title: toFixdLengthString * @Description: 将字符串用符号填充位数 * @param str 源字符串 * @param fixdlenth 位数 * @return String * @throws */ public static String toFixdLengthString(String str, int fixdlenth) { } /** * @Title: toFixdLengthString * @Description: 将数字用“0”填充位数 * @param num * @param fixdlenth * @return String * @throws */ public static String toFixdLengthString(int num, int fixdlenth) { } /** * @Title: generateSpaceString * @Description: 得到指定位数占位符 * @param length * @return String * @throws */ public static String generateSpaceString(int length) { } /** * @Title: generateZeroString * @Description: 得到指定位数的“0”的占位符 * @param length * @return String * @throws */ public static String generateZeroString(int length) { } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值