Math Random BigInteger BigDecimal

Math类

    math提供了系列的数学操作方法。所有的方法都是以静态的形式出现的

      1, math.PI   //取π

      2.math.max(1,2); //取大值

      3.math.round(89.9876);  //四舍五入

package org.lxh.mathdemo;

public class MathDemo {
	public static void main(String[] args) {
		System.out.println("PI = " + Math.PI);
		System.out.println(Math.max(1, 2));
		System.out.println(Math.round(89.9876));	// 四舍五入
	}

}

Random类(可以取得一系列的指定范围的随机数)

package org.lxh.randomdemo;

import java.util.Random;

public class RandomDemo {
	public static void main(String[] args) {
		Random r = new Random();
		for (int i = 0; i < 10; i++) {
			System.out.print(r.nextInt(100) + "、");
		}
	}

}


BigInteger类(表示大的整型数据)

package org.lxh.largenumberdemo;

import java.math.BigInteger;

public class BigIntegerDemo {
	public static void main(String[] args) {
		String num1 = "9999999999999999999999999999999999";
		String num2 = "9999999999999999999999999999999998";
		BigInteger big1 = new BigInteger(num1); // 实例化BigInteger对象
		BigInteger big2 = new BigInteger(num2); // 实例化BigInteger对象
		System.out.println("加法操作:" + big1.add(big2));
		System.out.println("减法操作:" + big1.subtract(big2));
		System.out.println("乘法操作:" + big1.multiply(big2));
		System.out.println("除法操作:" + big1.divide(big2));
		BigInteger result[] = big1.divideAndRemainder(big2);// 进行触发操作,有余数
		System.out.println("相除之后的商是:" + result[0]);
		System.out.println("相除之后的余数是:" + result[1]);
	}

}


 BigDecimal类主要是小数的大数计算,而且最重要的是可以精确到指定的四舍五入位数

       如果想要进行四舍五入的操作,则必须依靠以下的方法:

            public  BigDecimal  divide(BigDecimal divistor,int scale,int roundingMode)

                |-scale:表示四舍五入的位数


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值