Java数字处理类博

Random:
除Math之类中的random()方法可以获取随机数之外,java中还提供了一种可以获取随机数的方式,那就是java.util.Random类
0可以通过实例化一个Random对象创建一个随机数生成器。
public int nextInt(int n):返回大于等于0且小于N的随机整数。
public long nextLong():返回随机长整型值
public boolan nextBoolean():返回一个随机布尔型值
public float nextFloat();返回随机浮点型值
public double nextDouble():返回随机双精度型值
public double nextGaussian():返回一个概率密度为高斯分布的双精度。
双精度:而双精度double是用 8 个字节来存储的。
单精度:单精度,也就是 float ,在 32 位机器上用 4 个字节来存储的
单双精度:存储位不同,他们能表示的数值的范围就不同,也就是能准确表示的数的位数就不同。
public class RandomDemo_random类 {
 public static void main(String[] args) {
  //实例化一个Random类
  Random r=new Random();
       //随机产生一个整数
  System.out.println("随机产生一个大于等于0小于10的整数");
  //随机产生一个布尔型的值
  System.out.println("随机产生一个布尔型的值"+r.nextBoolean());
  //随机产生一个双精度的值
  System.out.println("随机产生一个双精度的值"+r.nextDouble());
  //随机产生一个浮点型的值
  System.out.println("随机产生一个浮点型的值"+r.nextFloat());
  //随机产生一个概率密度为高斯分布的双精度值
  System.out.println("随机产生一个概率密度为高斯分布的双精度值:"+r.nextGaussian());
 }
}
执行结果:
随机产生一个大于等于0小于10的整数
随机产生一个布尔型的值true
随机产生一个双精度的值0.061729159033922065
随机产生一个浮点型的值0.37646842
随机产生一个概率密度为高斯分布的双精度值:-0.8386717241588479
 
大数字运算:
BigInteger:BigInteger类型的数字范围较Integer类型的水准范围要大的多。Integer是int包装类型,int最大值为2的31次方-1,如果要计算更大的数字,使用Integer数据类型就
无法实现了,所以Java中提供了BigInteger类来处理更大的数字。BigInteger支持任意精度的整数,也就是说在运算中BigInteger类型可以准确地表示任何大小的整数值而不会丢失任何信息。
在BigInteger类中封装了多种操作,除了基本的加、减、乘、除操作之外,还提供了绝对值、相反数、最大公约数以及判断是否为质数等操作。
public BigInteger add(BigInteger val):做加法运算。
public BigInteger subtract(BigInteger val):做减法运算。
public BigInteger ,multiply(BigInteger val):做乘法运算
public BigInteger ,divide(BigInteger val):做除法运算
public BigInteger remainder(BigInteger val):做取余操作。
public BigInteger[] divideAndRemainder(BigInteger val):用数组返回余数和商,结果数组中第一个值为商,第二个值为余数。
public BigInteger pow(int exponent):进行取参数的exponent次方操作
public BigInteger negate():取相反数。
publiic BigInteger shiftLeft(int n):将数字左移n位,如果n为负数,做右移操作。
publiic BigInteger shiftRight(int n):将数字左移n位,如果n为负数,做左移操作。
public BigInteger and(BigInteger val):做与操作
public BigInteger or(BigInteger val):做或操作
public int compareTo(BigInteger val):做数字比较操作。
public boolean equals(Object x):当参数x是BigInteger类型的数字并且数值相等时,返回true。
public BigInteger min(BigInteger val):返回较小的数值
public BigInteger max(BigInteger val):返回较大的数值
下面就有BigInteger类型的实例化对象,调用该对象的各种方法实现大整数的加减乘除和其他运算结果
public class BigInteger_demo {
 public static void main(String[] args) {
  BigInteger b=new BigInteger("4");//实例化一个对象
  //取该大数字加2的操作
  System.out.println("加法操作"+b.add(new BigInteger("2")));
  //取该大数字乘以2的操作
  System.out.println("减法操作"+b.subtract(new BigInteger("2")));
  //取最大数乘以2的操作
  System.out.println("乘法操作"+b.multiply(new BigInteger("2")));
  //取最大数字除以2的操作
  System.out.println("除以2的操作"+b.divide(new BigInteger("2")));
  //取该最大数字的除以3的商
  System.out.println("取商"+b.divide(new BigInteger("2")));
  //取该最大数字的余数
  System.out.println("取该最大数字的2次方"+b.pow(2));
  //取该大数字的相反数
  System.out.println("取相反数操作"+b.negate());
 }
}
执行结果:
加法操作6
减法操作2
乘法操作8
除以2的操作2
取商2
取该最大数字的2次方16
取相反数操作-4
 
BigDecimal:
BigDecimal和BigInteger都能实现大数字的运算,不同的是BigDecimal加入了小数的概念。一般的float类型和double类型只可以用来做数学科学计算或工程计算,但由于在商业计算中要求数字精度比较高,所以要用到Java.math.BigDecimal类.BigDecimal类支持任何精度的定点数,可以用它来精确计算货币值。
public BigDecimal(double val):实例化时将双精度转换为BigDecimal类型。
public BigDecimal(String val):实例化时将字符串转换为BigDecimal类型。
加减乘除的方法
public BigDecimal add(BigDecimal augend):做加法操作。
public BigDecimal subtract(BigDecimal subtractrahend ):做减法操作
public BigDecimal multiply(BigDecimal multiplicand):做乘法操作
public BigDecimal divade(BigDecimal divisor,int scale,int roundingMode):做除法操作,方法中3个参数分别代表除数、商的小数点后的位数、近似处理模式。
下面做一个例子,给大家看看
public class BigDecimalDemo {
    static final int location=10;
    public BigDecimal add(double value1,double value2){
     BigDecimal b1=new BigDecimal(Double.toString(value1));
     BigDecimal b2=new BigDecimal(Double.toString(value2));
  return b1.add(b2);
    
    }
    public BigDecimal sub(double value1,double value2 ){
     BigDecimal b1=new BigDecimal(Double.toString(value1));
     BigDecimal b2=new BigDecimal(Double.toString(value2));
  return b1.subtract(b2);
    
    }
    public BigDecimal mul(double value1,double value2 ){
     BigDecimal b1=new BigDecimal(Double.toString(value1));
     BigDecimal b2=new BigDecimal(Double.toString(value2));
  return b1.multiply(b2);  
    }
    public BigDecimal div(double value1,double value2){
  return div(value1,value2,location);
    
    }
 private BigDecimal div(double value1, double value2, int b) {
  if(b<0){
   System.out.println("b值必须大于的等于0");
  }
  BigDecimal b1=new BigDecimal(Double.toString(value1));
     BigDecimal b2=new BigDecimal(Double.toString(value2));
     //调用除法方法,商消暑涤啊后保留B位b位,并将结果进行四舍五入
  return b1.divide(b2,b,BigDecimal.ROUND_HALF_UP);
 }
 public static void main(String[] args) {
  BigDecimalDemo b=new BigDecimalDemo();
    System.out.println("两个结果相加"+b.add(-7.5,8.9));
    System.out.println("两个结果相加"+b.sub(-7.5,8.9));
    System.out.println("两个结果相加"+b.mul(-7.5,8.9));
    System.out.println("两个结果相加"+b.div(-7.5,8.9));
 }
}
执行结果:
两个结果相加1.4
两个结果相加-16.4
两个结果相加-66.75
两个结果相加-0.8426966292
原文地址:https://www.cnblogs.com/wzhdcyy/p/9321455.html