Java常用工具类---数字工具类

本文详细介绍了Java中常用的数字工具类,包括其主要方法、功能及在实际开发中的应用示例,帮助读者深入理解如何高效处理数字操作。
摘要由CSDN通过智能技术生成
package com.jarvis.base.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Random;


public class NumericHelper {

	/**
	 * 描述:通过一个整数i获取你所要的哪几个(从0开始) i为 多个2的n次方之和,如i=7,那么根据原值是2的n次方之各,你的原值必定是1,2,4 。
	 * 
	 * @param i
	 *            数值
	 * @return
	 */
	public static int[] getWhich(long i) {
		int exp = Math.getExponent(i);
		if (i == (1 << (exp + 1)) - 1) {
			exp = exp + 1;
		}
		int[] num = new int[exp];
		int x = exp - 1;
		for (int n = 0; (1 << n) < i + 1; n++) {
			if ((1 << (n + 1)) > i && (1 << n) < (i + 1)) {
				num[x] = n;
				i -= 1 << n;
				n = 0;
				x--;
			}
		}
		return num;
	}

	/**
	 * 描述:非四舍五入取整处理
	 * 
	 * @param v
	 *            需要四舍五入的数字
	 * @return
	 */
	public static int roundDown(double v) {
		BigDecimal b = new BigDecimal(Double.toString(v));
		BigDecimal one = new BigDecimal("1");
		return b.divide(one, 0, BigDecimal.ROUND_DOWN).intValue();
	}

	/**
	 * 描述:四舍五入取整处理
	 * 
	 * @param v
	 *            需要四舍五入的数字
	 * @return
	 */
	public static int roundUp(double v) {
		BigDecimal b = new BigDecimal(Double.toString(v));
		BigDecimal one = new BigDecimal("1");
		return b.divide(o
/** * @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、付费专栏及课程。

余额充值