Java之 Math类

Number & Math 类方法
下面的表中列出的是 Number & Math 类常用的一些方法:

序号 方法与描述
1 xxxValue()
将 Number 对象转换为xxx数据类型的值并返回。
2 compareTo()
将number对象与参数比较。
3 equals()
判断number对象是否与参数相等。
4 valueOf()
返回一个 Number 对象指定的内置数据类型
5 toString()
以字符串形式返回值。
6 parseInt()
将字符串解析为int类型。
7 abs()
返回参数的绝对值。
8 ceil()
返回大于等于( >= )给定参数的的最小整数,类型为双精度浮点型。
9 floor()
返回小于等于(<=)给定参数的最大整数 。
10 rint()
返回与参数最接近的整数。返回类型为double。
11 round()
它表示四舍五入,算法为 Math.floor(x+0.5),即将原来的数字加上 0.5 后再向下取整,所以,Math.round(11.5) 的结果为12,Math.round(-11.5) 的结果为-11。
12 min()
返回两个参数中的最小值。
13 max()
返回两个参数中的最大值。
14 exp()
返回自然数底数e的参数次方。
15 log()
返回参数的自然数底数的对数值。
16 pow()
返回第一个参数的第二个参数次方。
17 sqrt()
求参数的算术平方根。
18 sin()
求指定double类型参数的正弦值。
19 cos()
求指定double类型参数的余弦值。
20 tan()
求指定double类型参数的正切值。
21 asin()
求指定double类型参数的反正弦值。
22 acos()
求指定double类型参数的反余弦值。
23 atan()
求指定double类型参数的反正切值。
24 atan2()
将笛卡尔坐标转换为极坐标,并返回极坐标的角度值。
25 toDegrees()
将参数转化为角度。
26 toRadians()
将角度转换为弧度。
27 random()
返回一个随机数。

/*

  • 构造方法:
  • public BigDecimal(String val)
  • public BigDecimal add(BigDecimal augend)
  • public BigDecimal subtract(BigDecimal subtrahend)
  • public BigDecimal multiply(BigDecimal multiplicand)
  • public BigDecimal divide(BigDecimal divisor)
  • public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):商,几位小数,如何舍取
import java.math.BigDecimal;
public class BigDecimalDemo {
    public static void main(String[] args) {
         System.out.println(0.09 + 0.01);
         System.out.println(1.0 - 0.32);
         System.out.println(1.015 * 100);
         System.out.println(1.301 / 100);
        System.out.println("-------------------");
        BigDecimal bd1 = new BigDecimal("0.09");
        BigDecimal bd2 = new BigDecimal("0.01");
        System.out.println("add:" + bd1.add(bd2));
        System.out.println("-------------------");

        BigDecimal bd3 = new BigDecimal("1.0");
        BigDecimal bd4 = new BigDecimal("0.32");
        System.out.println("subtract:" + bd3.subtract(bd4));
        System.out.println("-------------------");

        BigDecimal bd5 = new BigDecimal("1.015");
        BigDecimal bd6 = new BigDecimal("100");
        System.out.println("multiply:" + bd5.multiply(bd6));
        System.out.println("-------------------");

        BigDecimal bd7 = new BigDecimal("1.301");
        BigDecimal bd8 = new BigDecimal("100");
        System.out.println("divide:" + bd7.divide(bd8));
        System.out.println("divide:"
                + bd7.divide(bd8, 3, BigDecimal.ROUND_HALF_UP));
//		查看API:BigDecimal的ROUND_HALF_UP
        System.out.println("divide:"
                + bd7.divide(bd8, 8, BigDecimal.ROUND_HALF_UP));
    }
}

/*结果如下:

0.09999999999999999 
0.6799999999999999 
101.49999999999999 
0.013009999999999999 
------------------- 
add:0.10
 ------------------- 
 subtract:0.68
  ------------------- 
  multiply:101.500
   ------------------- 
   divide:0.01301
    divide:0.013
     divide:0.01301000 
     进程已结束,退出代码0
*/
  • public BigInteger add(BigInteger val):加
  • public BigInteger subtract(BigInteger val):减
  • public BigInteger multiply(BigInteger val):乘
  • public BigInteger divide(BigInteger val):除
  • public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组
import java.math.BigInteger;
public class BigIntegerDemo {
    public static void main(String[] args) {
        BigInteger bi1 = new BigInteger("100");
        BigInteger bi2 = new BigInteger("50");

        // public BigInteger add(BigInteger val):加
        System.out.println("add:" + bi1.add(bi2));
        // public BigInteger subtract(BigInteger val):加
        System.out.println("subtract:" + bi1.subtract(bi2));
        // public BigInteger multiply(BigInteger val):加
        System.out.println("multiply:" + bi1.multiply(bi2));
        // public BigInteger divide(BigInteger val):加
        System.out.println("divide:" + bi1.divide(bi2));

        // public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组
        BigInteger[] bis = bi1.divideAndRemainder(bi2);
        System.out.println("商:" + bis[0]);
        System.out.println("余数:" + bis[1]);
    }
}
/*
add:150
subtract:50
multiply:5000
divide:2
商:2
余数:0

进程已结束,退出代码0

*/
  • Math:用于数学运算的类。
  • 成员变量:* public static final double PI
  •  public static final double E
    
  • 成员方法:
  •  public static int abs(int a):绝对值
    
  •  public static double ceil(double a):向上取整
    
  •  public static double floor(double a):向下取整
    
  •  public static int max(int a,int b):最大值 (min自学)
    
  •  public static double pow(double a,double b):a的b次幂
    
  •  public static double random():随机数 [0.0,1.0)
    
  •  public static int round(float a) 四舍五入(参数为double的自学)
    
  •  public static double sqrt(double a):正平方根
    
public class MathDemo {
    public static void main(String[] args) {
        // public static final double PI
        System.out.println("PI:" + Math.PI);
        // public static final double E
        System.out.println("E:" + Math.E);
        System.out.println("--------------");

        // public static int abs(int a):绝对值
        System.out.println("abs:" + Math.abs(10));
        System.out.println("abs:" + Math.abs(-10));
        System.out.println("--------------");

        // public static double ceil(double a):向上取整
        System.out.println("ceil:" + Math.ceil(12.34));
        System.out.println("ceil:" + Math.ceil(12.56));
        System.out.println("--------------");

        // public static double floor(double a):向下取整
        System.out.println("floor:" + Math.floor(12.34));
        System.out.println("floor:" + Math.floor(12.56));
        System.out.println("--------------");

        // public static int max(int a,int b):最大值
        System.out.println("max:" + Math.max(12, 23));
        // 需求:我要获取三个数据中的最大值
        // 方法的嵌套调用
        System.out.println("max:" + Math.max(Math.max(12, 23), 18));
        // 需求:我要获取四个数据中的最大值
        System.out.println("max:"
                + Math.max(Math.max(12, 78), Math.max(34, 56)));
        System.out.println("--------------");

        // public static double pow(double a,double b):a的b次幂
        System.out.println("pow:" + Math.pow(2, 3));
        System.out.println("--------------");

        // public static double random():随机数 [0.0,1.0)
        System.out.println("random:" + Math.random());
        // 获取一个1-100之间的随机数
        System.out.println("random:" + ((int) (Math.random() * 100) + 1));
        System.out.println("--------------");

        // public static int round(float a) 四舍五入(参数为double的自学)
        System.out.println("round:" + Math.round(12.34f));
        System.out.println("round:" + Math.round(12.56f));
        System.out.println("--------------");

        //public static double sqrt(double a):正平方根
        System.out.println("sqrt:"+Math.sqrt(4));
    }
}
/*
PI:3.141592653589793
E:2.718281828459045
--------------
abs:10
abs:10
--------------
ceil:13.0
ceil:13.0
--------------
floor:12.0
floor:12.0
--------------
max:23
max:23
max:78
--------------
pow:8.0
--------------
random:0.19701630066306275
random:79
--------------
round:12
round:13
--------------
sqrt:2.0

进程已结束,退出代码0

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值