Java中数字操作

public static void main(String[] args) throws Exception
{
    {
        //Math函数的四舍五入,注意负数的时候小数位<=0.5都会被舍去,>0.5的才会被进位
        System.out.println(Math.round(15.5));
        System.out.println(Math.round(-15.5));
        System.out.println(Math.round(-15.51));
    }
    {
        //用Random类生成随机数
        List<Integer> intList = new ArrayList<Integer>();
        Random random = new Random();
        int count = 0;
        while(count <= 7)
        {
            Integer tem = random.nextInt(37);
            if(intList.contains(tem))
                continue;
            else
            {
                intList.add(tem);
                count++; 
            }
        }
        
        System.out.println(intList);
    }
    {
        //大整数的加减乘除
        BigInteger bigA = new BigInteger("132122332323232323");
        BigInteger bigB = new BigInteger("45648945649874645645645641231974816");
        System.out.println(bigA);
        System.out.println(bigB);
        System.out.println(bigA.multiply(bigB));
    }
    {
        //大小数的四舍五入
        BigDecimal bd = new BigDecimal("12358645.22564");
        System.out.println(bd.divide(new BigDecimal("1"), 1, BigDecimal.ROUND_HALF_UP));;
    }
}

 

posted on 2016-10-01 18:02  kuillldan  阅读(221)  评论(0编辑  收藏  举报