Java 数学操作类

数学操作类

Math类 数学计算操作类

类属性值

  • Math.E

    ^

  • Math.PI

    圆周率

类方法

Math类中,一切方法都是 static 型,因为Math类中没有普通属性。

round() 方法

  • 四舍五入,返回最接近int值的参数
public static int round(float a)

abs() 方法

  • 返回绝对值
public static double abs(double a)

max() 方法

  • 返回int值中较大的那个值
public static int max(int a , int b)

Random类 随机操作类

  • java.util 包中

Random() 构造

  • 创建一个新的随机数生成器

next() 方法

  • 生成下一个伪随机数
protected int next (int bits)

nextInt() 方法

  • 返回下一个伪随机数

  • nextInt(int n)
    • 返回 小于 n之内的随机数

36选7 彩票器实例

import java.util.Random;

public class TestDemo {
    public static void main(String [] args) throws CloneNotSupportedException {
        Random ran = new Random();
        int data[] = new int [7] ; //开辟一个数组
        int foot = 0 ;
        while (foot < 7) {
            int t = ran.nextInt(37); // 随机生成返回一个不大于37的数
            if (!isRepeat(data,t)) { // 查重
                data[foot ++] = t ;
            }
        }
        java.util.Arrays.parallelSort(data);
        for (int x = 0 ; x < data.length ; x ++) {
            System.out.print(data[x] + "\t");
        }
    }
    public static boolean isRepeat(int temp[] , int num) { // 查重
        if (num == 0 ) {
            return true ;
        }
        for (int x = 0 ; x < temp.length ; x ++) {
            if (temp[x] == num) {
                return true ;
            }
        }
        return false;
    }
}

大数字操作类

BigInteger 类

import java.math.BigInteger;
import java.util.Random;

public class TestDemo {
    public static void main(String [] args) throws CloneNotSupportedException {
        BigInteger big_A = new BigInteger("12345678912345678912356789");
        BigInteger big_B = new BigInteger("218372948729847298347289") ;
        System.out.println("加法操作 >>> " + (big_A.add(big_B)));
        System.out.println("减法操作 >>> " + (big_A.subtract(big_B)));
        System.out.println("乘法操作 >>> " + (big_A.multiply(big_B)));
        System.out.println("除法操作 >>> " + (big_A.divide(big_B)));
    }
}
  • 运行结果:
加法操作 >>> 12564051861075526210704078
减法操作 >>> 12127305963615831614009500
乘法操作 >>> 2695962308160819899591376721692771747399598895021
除法操作 >>> 56

BigDecimal : 大浮点数

  • BigInteger只可以保存整数,不可以保存小数(浮点数),而BigDecimal可以保存小数(浮点)数据;在BigDecimal类提供如下构造:
public BigDecimal(String val) ;
public BigDecimal(double val) ; 

Math.round()方法虽然实现四舍五入操作,但是,小数在计算的时候会自动的四舍五入

除法操作

public BigDecimal divide(BigDecimal divisor , int scale , int round);
  • BigDecimal divsor : 被除数
  • int scale:保留的小数位
  • int round:进位模式

  • 实例:*【重要内容】

import java.math.BigDecimal;

class MyMath {
    /**
     * 实现准确的位数的四舍五入操作
     * @param num 进行四舍五入操作的数字
     * @param scale 要保留的小数位数
     * @return 处理后的数据
     */
    public static double round(double num , int scale) {
        BigDecimal bigA = new BigDecimal(num) ;
        BigDecimal bigB = new BigDecimal(1) ;
        // ROUND_HALF_UP:向“最接近的”数字舍入
        // doubleValue() : 转 double 型数据
        return bigA.divide(bigB, scale , BigDecimal.ROUND_HALF_UP).doubleValue() ;
    }
}

public class TestDemo {
    public static void main(String [] args) {
        System.out.println(MyMath.round(19.224532 , 2));
        System.out.println(MyMath.round(3.1465926 , 2));
    }
}

总结:

  • Math类要清楚round()方法的缺陷
  • Random类生成随机数
  • 如果处理大量的数据量,则使用 BigInteger和BigDecimal ,两个类都属于Number的子类

猜你喜欢

转载自www.cnblogs.com/wangyuyang1016/p/11105291.html