java 數字處理工具類

package com.lyis.commons.util;

import java.math.BigDecimal;
import java.util.regex.Pattern;

/**
 * 數字處理工具類
 * 
 * @author Johnson
 * @version Monday October 25th, 2010
 */
public class NumberUtils {

	/**
	 * 判斷當前值是否爲整數
	 * 
	 * @param value
	 * @return
	 */
	public static boolean isInteger(Object value) {
		if (StringUtils.isEmpty(value)) {
			return false;
		}
		String mstr = value.toString();
		Pattern pattern = Pattern.compile("^-?\\d+{1}quot;);
		return pattern.matcher(mstr).matches();
	}

	/**
	 * 判斷當前值是否爲數字(包括小數)
	 * 
	 * @param value
	 * @return
	 */
	public static boolean isDigit(Object value) {
		if (StringUtils.isEmpty(value)) {
			return false;
		}
		String mstr = value.toString();
		Pattern pattern = Pattern.compile("^-?[0-9]*.?[0-9]*{1}quot;);
		return pattern.matcher(mstr).matches();
	}

	/**
	 * 將數字格式化輸出
	 * 
	 * @param value
	 *            需要格式化的值
	 * @param precision
	 *            精度(小數點後的位數)
	 * @return
	 */
	public static String format(Object value, Integer precision) {
		Double number = 0.0;
		if (NumberUtils.isDigit(value)) {
			number = new Double(value.toString());
		}
		precision = (precision == null || precision < 0) ? 2 : precision;
		BigDecimal bigDecimal = new BigDecimal(number);
		return bigDecimal.setScale(precision, BigDecimal.ROUND_HALF_UP)
				.toString();
	}

	/**
	 * 將數字格式化輸出
	 * 
	 * @param value
	 * @return
	 */
	public static String format(Object value) {
		return NumberUtils.format(value, 2);
	}

	/**
	 * 將值轉成Integer型,如果不是整數,則返回0
	 * 
	 * @param value
	 * @param replace
	 *            如果爲0或者null,替換值
	 * @return
	 */
	public static Integer parseInteger(Object value, Integer replace) {
		if (!NumberUtils.isInteger(value)) {
			return replace;
		}
		return new Integer(value.toString());
	}

	/**
	 * 將值轉成Integer型,如果不是整數,則返回0
	 * 
	 * @param value
	 * @return
	 */
	public static Integer parseInteger(Object value) {
		return NumberUtils.parseInteger(value, 0);
	}

	/**
	 * 將值轉成Long型
	 * 
	 * @param value
	 * @param replace
	 *            如果爲0或者null,替換值
	 * @return
	 */
	public static Long parseLong(Object value, Long replace) {
		if (!NumberUtils.isInteger(value)) {
			return replace;
		}
		return new Long(value.toString());
	}

	/**
	 * 將值轉成Long型,如果不是整數,則返回0
	 * 
	 * @param value
	 * @return
	 */
	public static Long parseLong(Object value) {
		return NumberUtils.parseLong(value, 0L);
	}

	/**
	 * 將值轉成Double型
	 * 
	 * @param value
	 * @param replace
	 *            replace 如果爲0或者null,替換值
	 * @return
	 */
	public static Double parseDouble(Object value, Double replace) {
		if (!NumberUtils.isDigit(value)) {
			return replace;
		}
		return new Double(value.toString());
	}

	/**
	 * 將值轉成Double型,如果不是整數,則返回0
	 * 
	 * @param value
	 * @return
	 */
	public static Double parseDouble(Object value) {
		return NumberUtils.parseDouble(value, 0.0);
	}

	/**
	 * 將char型數據轉成字節數組
	 * 
	 * @param value
	 * @return
	 */
	public static byte[] toBytes(char value) {
		byte[] bt = new byte[2];
		for (int i = 0; i < bt.length; i++) {
			bt[i] = (byte) (value >>> (i * 8));
		}
		return bt;
	}

	/**
	 * 將short型數據轉成字節數組
	 * 
	 * @param value
	 * @return
	 */
	public static byte[] toBytes(short value) {
		byte[] bt = new byte[2];
		for (int i = 0; i < bt.length; i++) {
			bt[i] = (byte) (value >>> (i * 8));
		}
		return bt;
	}

	/**
	 * 將int型數據轉成字節數組
	 * 
	 * @param value
	 * @return
	 */
	public static byte[] toBytes(int value) {
		byte[] bt = new byte[4];
		for (int i = 0; i < bt.length; i++) {
			bt[i] = (byte) (value >>> (i * 8));
		}
		return bt;
	}

	/**
	 * 將long型數據轉成字節數組
	 * 
	 * @param value
	 * @return
	 */
	public static byte[] toBytes(long value) {
		byte[] bt = new byte[8];
		for (int i = 0; i < bt.length; i++) {
			bt[i] = (byte) (value >>> (i * 8));
		}
		return bt;
	}

	/**
	 * 將short型數據插入到指定索引的字節數組中
	 * 
	 * @param index
	 *            索引
	 * @param values
	 *            字節數組
	 * @param value
	 *            需要插入的值
	 */
	public static void insert(int index, byte[] values, short value) {
		byte[] bt = NumberUtils.toBytes(value);
		System.arraycopy(bt, 0, values, index, 2);
	}

	/**
	 * 將int型數據插入到指定索引的字節數組中
	 * 
	 * @param index
	 *            索引
	 * @param values
	 *            字節數組
	 * @param value
	 *            需要插入的值
	 */
	public static void insert(int index, byte[] values, int value) {
		byte[] bt = NumberUtils.toBytes(value);
		System.arraycopy(bt, 0, values, index, 4);
	}

	/**
	 * 將long型數據插入到指定索引的字節數組中
	 * 
	 * @param index
	 *            索引
	 * @param values
	 *            字節數組
	 * @param value
	 *            需要插入的值
	 */
	public static void insert(int index, byte[] values, long value) {
		byte[] bt = NumberUtils.toBytes(value);
		System.arraycopy(bt, 0, values, index, 8);
	}

	/**
	 * 將字節轉換成整型
	 * 
	 * @param value
	 *            字節類型值
	 * @return
	 */
	public static int byteToInt(byte value) {
		if (value < 0) {
			return value + 256;
		}
		return value;
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章