Java的数字操作类
一.Math类
Math类是一个专门用来进行数学计算的操作类,它提供了一系列的数学计算方法。在Math类里面提供的一切方法都是static型方法,所以可以直接由类名称进行调用。
例:观察四舍五入操作
package Project.Study.MathClass;
public class Test1 {
public static void main(String[]args){
System.out.println(Math.round(15.5));
System.out.println(Math.round(-15.51));
System.out.println(Math.round(-15.4));
System.out.println(Math.round(-15.45));
System.out.println(Math.round(-15.6));
System.out.println(Math.round(15.46));
System.out.println(Math.round(-15.5));
}
}
//结果:
//16
//-16
//-15
//-15
//-16
//15
//-15
由上程序我们可以发现,只有当操作数据小数位大于0.5才进位,小于或等于0.5则不进位。
例:实现指定位数的四舍五入操作
package Project.Study.MathClass;
public class Test2 {
public static void main(String[]args){
System.out.println(round(-15.678139,2));
}
/**
* 四舍五入操作,可以保留指定长度的小数位数
* @param num 要进行四舍五入操作的数字
* @param scale 保留的小数位
* @return 四舍五入后的数据
*/
public static double round(double num,int scale){
//Math.pow()的方法作用是进行10的N次方的计算
return Math.round(num*Math.pow(10.0,scale))/Math.pow(10.0,scale);
}
}
//结果:
//-15.68
二.Random类
Random类是一个专门负责产生随机数的操作类。
Random类的常用方法:
No. | 方法 | 类型 | 描述 |
1 | public Random() | 构造 | 创建一个新的Random实例 |
2 | public Random(long seed) | 构造 | 创建一个新的Random实例并且设置一个种子数 |
3 | public int nextInt(int bound) | 普通 | 产生一个不大于指定边界的随机整数 |
例:产生10个不大于100的正整数
package Project.Study.RandomClass;
import java.util.Random;
public class Test1 {
public static void main(String[]args){
Random rand=new Random();
for(int x=0;x<10;x++){
System.out.print(rand.nextInt(100)+"、");
}
}
}
//结果:
//89、93、9、96、33、76、10、80、41、87、
例:选号系统(36选7)
package Project.Study.RandomClass;
import java.util.Random;
public class Test2 {
public static void main(String[]args){
Random rand=new Random();
int[] data =new int[7]; //开辟一个7个元素的数组,保存生成数字
int foot=0; //数组操作脚标
while(foot<7){
int t=rand.nextInt(37);//生成一个不大于37的随机数
if(!isRepeat(data,t)){ //重复的话就不执行
data[foot++]=t; //保存数据
}
}
java.util.Arrays.sort(data);//排序
for (int datum : data) {
System.out.print(datum + "、");
}
}
/**
* 此方法主要是判断是否存在重复内容
* @param temp 指的是已经保存的数据
* @param num 新生成的数据
* @return 如果存在返回true,否则返回false
*/
public static boolean isRepeat(int[] temp, int num){
if(num==0){ //没有必要判断了
return true; //直接返回,随后的代码都不再执行
}
for (int i : temp) {
if (i == num) {
return true;//表示后面的数据都不再进行判断
}
}
return false;
}
}
//结果:
//2、9、12、13、18、28、31、
三.大数字操作类
1.大整数操作类:BigInteger
大整数可以操作无穷大的整型数据。
BigInteger类的基本操作方法:
No. | 方法 | 类型 | 描述 |
1 | public BigInteger(String val) | 构造 | 实例化BigInteger对象 |
2 | public BigInteger add(BigInteger val) | 普通 | 加法操作 |
3 | public BigInteger subtract(BigInteger val) | 普通 | 减法操作 |
4 | public BigInteger multiply(BigInteger val) | 普通 | 乘法操作 |
5 | public BigInteger divide(BigInteger val) | 普通 | 除法操作(不保留余数) |
6 | public BigInteger [] divideAndRemainder(BigInteger val) | 普通 | 除法操作(保留余数),数组第一个元素是商,第二个元素是余数 |
例:进行大整数计算
package Project.Study.BigIntegerClass;
import java.math.BigInteger;
public class Test1 {
public static void main(String[]args){
BigInteger bigA=new BigInteger("123456789013213214214"); //大数字A
BigInteger bigB=new BigInteger("2324342314132432432"); //大数字B
System.out.println("加法操作:"+bigA.add(bigB));
System.out.println("减法操作:"+bigA.subtract(bigB));
System.out.println("乘法操作:"+bigA.multiply(bigB));
System.out.println("除法操作:"+bigA.divide(bigB));
BigInteger result[]=bigA.divideAndRemainder(bigB);
System.out.println("商:"+result[0]+",余数:"+result[1]);
}
}
//结果:
//加法操作:125781131327345646646
//减法操作:121132446699080781782
//乘法操作:286955838670331461717477157288896988448
//除法操作:53
//商:53,余数:266646364194295318
2.大小数操作类:BigDecimal
使用BigDecimal类最常进行的操作便是进行准确位数的四舍五入计算。
使用BigDecimal完成四舍五入操作:
No. | 方法及常量 | 类型 | 描述 |
1 | public static final int ROUND_HALF_UP | 常量 | 向上进位 |
2 | public BigDecimal(double val) | 构造 | 传递一个double型数据 |
3 | public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode) | 普通 | 除法操作,参考意义如下:BigDecimal divisor:被除数; int scale:保留的小数位长度;int roundingMode: 进位模式 |
例:完成准确的四舍五入操作
ckage Project.Study.BigDecimalClass;
import java.math.BigDecimal;
class MyMath{
public static double round(double num,int scale){
BigDecimal big=new BigDecimal(num); //将数据封装在BigDecimal类中
BigDecimal result= big.divide(new BigDecimal(1), scale, BigDecimal.ROUND_HALF_UP); //除法计算
return result.doubleValue(); //Number类的方法
}
}
public class Test1 {
public static void main(String[]args){
System.out.println(MyMath.round(15.5,0));
System.out.println(MyMath.round(-15.5,0));
System.out.println(MyMath.round(1168.4344343,4));
}
}
//结果:
//16.0
//-16.0
//1168.4344