Math类是一个专门进行数学计算的类,里面提供一系列的数学计算方法
Math类里面提供一切方法都是static型的方法,因为Math类没有普通属性
1.数学的四舍五入
格式: public static long round(double a)
例:
public class Nice {
public static void main(String args[]) {
System.out.println(Math.round(15.5)) ; //16
System.out.println(Math.round(-15.5)) ; //-15
System.out.println(Math.round(-15.51)) ; //-16
}
}
//负数四舍五入时,数据小数位大于0.5才进位,小于等于0.5不进位
2.随机数的产生
java.util.Random,可以产生随机数
例:
import java.util.Random;
public class Nice {
public static void main(String args[]) {
Random rand = new Random() ;
for(int x=0 ; x < 10 ; x++) {
System.out.print(rand.nextInt(100) + "、");
}
}
}
//设计一个选号码程序,并且排好大小顺序
import java.util.Random;
public class Nice {
public static void main(String args[]) {
Random rand = new Random() ;
int data[] = new int[10] ; //我要选十个数字
int foot = 0 ;
while ( foot < 10) { //因为不知道次数,所以使用while
int t = rand.nextInt(100) ; //100为上限
if(!isRepeat(data,t)) { //重复
data[foot++] = t ;
}
}
java.util.Arrays.sort(data);
for(int x=0 ; x < data.length ; x++) {
System.out.print(data[x] + "、");
}
}
/*
*此方法为判断是否有重复或者为0的内容
*存在的话 返回ture ,否则为false
*/
public static boolean isRepeat(int temp[], int num) {
if(num == 0) { //没有必要判断
return true ; //返回flase,不再执行
}
for(int x = 0 ; x < temp.length ; x++) {
if(temp[x] == num) {
return true ;
}
}
return false ;
}
}
3.大数字的表示
大数字是什么?看下面例子:
public class Nice {
public static void main(String args[]) {
System.out.println(Double.MAX_VALUE * Double.MAX_VALUE); //Infinity
}
}
//超出了double的范围
为了有超过double范围的数字可以计算,大数字的操作类就有 BigInteger、BigDecimal 两种
1)BigInteger
构造方法: public BigInteger(String val), 接收的是String型,是为了避免数字过大,无法表示
(BigInteger 基本的数学计算都可以操作)
import java.math.BigInteger ;
public class Nice {
public static void main(String args[]) {
BigInteger bigA = new BigInteger("64646511348");
BigInteger bigB = new BigInteger("4321346654") ;
System.out.println("加法操作:" + (bigA.add(bigB))) ;
System.out.println("减法操作:" + (bigA.subtract(bigB))) ;
System.out.println("乘法操作:" + (bigA.multiply(bigB))) ;
System.out.println("加法操作:" + (bigA.divide(bigB))) ;
//result数组里只有两个元素,一个是商,一个是余数
BigInteger result [] = bigA.divideAndRemainder(bigB) ;
System.out.println("商:" + result[0] + ",余数:" + result[1]);
}
}
2)BigDecimal
BigInteger 不可以保存小数,BigDecimal可以保存小数
1.构造一: public BigDecimal(String val);
2.构造二: public BigDecimal(double val);
也可以进行基本基础数学计算,还可以用它来实现精准的四舍五入操作。
没有直接支持的四舍五入的方法,可以用除法实现:
除法操作: public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
1.BigDecimal divisor: 被除数
2.int scale: 保留的小数位
3.int roundingMode: 进位模式(public static final int ROUND_HALF_UP这个常量就是描述进位模式的,四舍五入的向上进位方式)
import java.math.BigDecimal ;
class MyMath { //实现精准的四舍五入操作
public static double round(double num,int scale) {
BigDecimal bigA = new BigDecimal(num) ;
BigDecimal bigB = new BigDecimal(1) ;
return bigA.divide(bigB,scale,BigDecimal.ROUND_HALF_UP).doubleValue() ;
}
}
public class Nice {
public static void main(String args[]) throws Exception{
System.out.println(MyMath.round(19.64646987,2)) ; //保留2位
System.out.println(MyMath.round(-传递传递15.5,0)) ; //现在就是-16.0了,正常的思维
}
}