Math Random BigInteger BigDecimal

来源:互联网 发布:淘宝怎么转人工服务 编辑:程序博客网 时间:2024/05/23 19:20

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:表示四舍五入的位数


 

原创粉丝点击