数字操作类
程序就是一个数学的处理过程,所以Java本身也提供有相应的数字处理的类库支持。
Math数学计算类
Math类的主要功能是进行数学计算的操作类,提供有基础的计算公式操作。这个类的构造方法被私有化了,而且该类之中提供的所有方法都是static方法,即这些方法都可以通过类名称直接调用。
- Math.abs(a);
- Math.max(a,b);
- Math.log(a);
- Math.round(a);
- Math.pow(a,b);
&emsp虽然Math里面提供有四舍五入的处理方法,但是这个四舍五入在进行处理时直接将小数点后的所有位进行进位处理,那么现在最方便的做法是实现指定位数的保留。
自定义四舍五入
class MathUtil{
private MathUtil() {
}
/**
* 实现数据四舍五入,
* @param num 操作数
* @param scale 四舍五入保留的小数位数
* @return 处理后的结果
*/
public static double round(double num, int scale) {
return Math.round(num * Math.pow(10, scale)) / Math.pow(10, scale);
}
}
public class MathStudy {
public static void main(String[] args) {
System.out.println(MathUtil.round(19.86273, 2));
}
}
Math中提供的都是基础的数学公式,需要的话可进行整合。
Random随机数生成类
java.util.Random类的主要功能是产生随机数的,该类主要是依靠内部提供的方法来完成:
- 产生一个不大于边界的随机正整数:
public int nextInt(int bound);
彩票Demo
import java.util.Arrays;
import java.util.Random;
public class MathStudy {
public static void main(String[] args) {
int data [] = new int [7];
Random rand = new Random();
int foot = 0; //操作脚标
while(foot < 7) {
//选择7个数字
int num = rand.nextInt(37); //生成一个不大于37的正整数
if(isUse(num, data)) {
//改数字可以使用
data[foot++] = num;
}
}
Arrays.sort(data);
for(int x=0;x<data.length;x++) {
System.out.print(data[x]+"、");
}
}
/**
* 判断传入数字是否为0以及是否在数组中存在
* @param num 传入数字
* @param temp 已经存在的数据
* @return
*/
public static boolean isUse(int num, int temp[]) {
if(num == 0) {
return false;
}
for(int x =0; x<temp.length;x++) {
if(num == temp[x]) {
return false;
}
}
return true;
}
}
大数字处理类
在进行数学计算的过程中,大数字处理类可以实现海量数字的计算(能提供的也只是基础计算),假设一个数超过Double范围,此时并没有一种数据类型可以保存下此类的内容,最早的时候只能通过String保存,这样开发难度很高,为了解决这个问题,提供两个大数字操作类:BigInteger、BigDecimal;
两个大数字处理类的构造方法:
- BigInteger:
public BigInteger(String val);
- BigDecimal:
public BigDecimal(String val);
public class MathStudy {
public static void main(String[] args) {
BigInteger bigA = new BigInteger("2345345654885795785467");
BigInteger bigB = new BigInteger("23453465488577");
System.out.println(bigA.add(bigB));
System.out.println(bigA.subtract(bigB));
System.out.println(bigA.divide(bigB));
System.out.println(bigA.multiply(bigB));
}
}
需要注意的是,虽然提供有大数字操作类,但是整体的操作之中需要考虑到性能问题。
既然在数学除法中无法做到整除,那么可以进行简化,使用其他的除法计算求余数:
- 求余:
public BigInteger[] divideAndReainder(BigInteger val);
数组第一个元素为商,第二个元素为余数
public class MathStudy {
public static void main(String[] args) {
BigInteger bigA = new BigInteger("2345345654885795785467");
BigInteger bigB = new BigInteger("23453465488577");
BigInteger result [] = bigA.divideAndRemainder(bigB);
System.out.println("商"+result[0] +"、余数"+result[1]);
}
}
如果在开发中进行计算的时候,该计算没有超过基本数据类型所包含的位数,不建议使用大数字类,因为这种计算性能很差。
BigDecimal操作形式和BigInteger是非常类似的,都有基础的数学支持;
使用BigDecimal计算
public class MathStudy {
public static void main(String[] args) {
BigDecimal bigA = new BigDecimal("2345345654885795785467");
BigDecimal bigB = new BigDecimal("23453465488577");
System.out.println(bigA.add(bigB));
System.out.println(bigA.subtract(bigB));
BigDecimal result [] = bigA.divideAndRemainder(bigB);
System.out.println("商"+result[0] +"、余数"+result[1]);
}
}
在使用BigDecimal的时候有一个数据进位的问题,在这个类里面定义有如下的除法计算:
- 除法:
public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode);
使用BigDecimal实现四舍五入处理
package per.lyz.BasicClass_study;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Random;
class MathUtil{
private MathUtil() {
}
/**
* 实现数据四舍五入,
* @param num 操作数
* @param scale 四舍五入保留的小数位数
* @return 处理后的结果
*/
public static double round_1(double num,int scale) {
return new BigDecimal(num).divide(new BigDecimal(1.0), scale, RoundingMode.HALF_UP).doubleValue();
}
}
public class MathStudy {
public static void main(String[] args) {
System.out.println(MathUtil.round_1(19.56777, 2));
}
}
Math的处理由于使用的都是基本数据类型,所以性能一定要高于大数字处理类。