Java经常使用数学方法

/** * 千分位数据格式化 * * @param text * @return */
	public static String format(String text) {
		DecimalFormat df = null;
		if (text.indexOf(".") > 0) {
			if (text.length() - text.indexOf(".") - 1 == 0) {
				df = new DecimalFormat("###,##0.");
			} else if (text.length() - text.indexOf(".") - 1 == 1) {
				df = new DecimalFormat("###,##0.0");
			} else {
				df = new DecimalFormat("###,##0.00");
			}
		} else {
			df = new DecimalFormat("###,##0");
		}

		BigDecimal number = BigDecimal.ZERO;
		try {
			number = new BigDecimal(text);
		} catch (Exception e) {
			number = BigDecimal.ZERO;
		}
		return df.format(number);
	}

	/** * 带有精度的四舍五入 * * @param number * @param scale * @return */
	public static BigDecimal scale(BigDecimal number, int scale) {
		return number.setScale(scale, RoundingMode.UP);
	}

	/** * 当前值的N次方 * * @param number * @param power * @return */
	public static BigDecimal power(BigDecimal number, int power) {
		// Math.pow(number.doubleValue(), power)存在精度问题
		BigDecimal bd = BigDecimal.ONE;
		while (power > 0) {
			bd = bd.multiply(number);
			--power;
		}
		return bd;
	}