大数类
当我们编程中处理的数字比较大或者精度很高 , 此时long/double不够用就可以使用BigInteger/BigDecimal来处理(专门用在财务软件当中)
- BigInteger/BigDecimal不属于基本数据类型,属于引用数据类型
- BigDecimal/BigInteger的数字是对象 , 在对其进行加减乘除的时候也不会拆箱故不能直接进行 + - * / , 需要使用对应的方法
java.math.BigInteger(大整数)
方法名 | 功能 |
---|
BigInteger(“”) | 把数据当成字符串处理 , 然后封装成BigInteger类型的对象 |
BigInteger add(BigInteger) | 对两个BigInteger类型的数字进行相加 |
BigInteger subtract(BigInteger) | 对两个BigInteger类型的数字进行相减 |
BigInteger multiply(BigInteger) | 对两个BigInteger类型的数字进行相乘 |
BigInteger divide(BigInteger) | 对两个BigInteger类型的数字进行相除 |
public class BigInteger_ {
public static void main(String[] args) {
BigInteger bigInteger = new BigInteger("23788888899999999999999999999");
BigInteger bigInteger2 = new BigInteger("10099999999999999999999999999999999999999999999999999999999");
BigInteger addResult = bigInteger.add(bigInteger2);
System.out.println(addResult);
BigInteger subtractResult = bigInteger.subtract(bigInteger2);
System.out.println(subtractResult);
BigInteger multiplyResult = bigInteger.multiply(bigInteger2);
System.out.println(multiplyResult);
BigInteger divideResult = bigInteger.divide(bigInteger2);
System.out.println(divideResult);
}
}
java.math.BigDecimal(大浮点数)
方法名 | 功能 |
---|
BigDecimal(“”) | 把数据当成字符串处理 , 然后封装成BigDecimal类型的对象 |
BigDecimal add(BigDecimal) | 对两个 BigDecimal 类型的数字进行相加 |
BigDecimal subtract(BigDecimal) | 对两个 BigDecimal类型的数字进行相减 |
BigDecimal multiply(BigDecimal) | 对两个 BigDecimal类型的数字进行相乘 |
BigDecimal divide(BigDecimal , BigDecimal.ROUND_CEILING) | 对两个 BigDecimal类型的数字进行相除 , 如果除不尽会抛出异常 , 一般需要指定结果的精度 |
public class BigDecimal_ {
public static void main(String[] args) {
double d = 1999.11111111111999999999999977788d;
BigDecimal bigDecimal = new BigDecimal("1999.11111111111999999999999977788");
BigDecimal bigDecimal2 = new BigDecimal("3");
System.out.println(bigDecimal.add(bigDecimal2));
System.out.println(bigDecimal.subtract(bigDecimal2));
System.out.println(bigDecimal.multiply(bigDecimal2));
System.out.println(bigDecimal.divide(bigDecimal2, BigDecimal.ROUND_CEILING));
}
}
java.text.DecimalFormat格式化
java.text.DecimalFormat专门负责数字格式化的类(格式化三要素long/double类型的数字,数字格式化类,调用format方法格式化)
- "#"代表任意数字, " , "代表千分位, "."代表小数点 , "0"代表保留小数位的个数不够时补0
方法名 | 功能 |
---|
DecimalFormat(“数字格式”) | 创建一个对象用来可以格式化指定数字 |
String format( long / double) | 按照规定格式化long类型或者double类型的数字 , 返回一个字符串 |
public class DecimalFormatTest01 {
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("###,###.##");
String s = df.format(1234.561232);
System.out.println(s);
DecimalFormat df2 = new DecimalFormat("###,###.0000");
String s2 = df2.format(1234.56);
System.out.println(s2);
}
}
java.lang.Math(数学运算)
常用的静态方法
Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数
方法名 | 功能 |
---|
abs(数字类型) | 返回数字的绝对值 |
pow(int , int ) | 对指定数字进行指定的幂运算 |
double ceil(double) | 向上取整 , 返回大于等于该参数的最小整数并转成double类型 |
double floor(double) | 向下取整 , 返回小于等于该参数的最大整数并转成double类型 |
long round(double) | 四舍五入 , 返回对参数四舍五入后的值并转成long类型 |
double sqrt(double) | 对数字求开方并转成double类型 |
min(int,int) | 返回两个数字之间的最小值 |
max(int,int) | 返回两个数字之间的最大值 |
random() | 返回 0 <= x < 1 之间的一个随机小数 |
public class MathMethod {
public static void main(String[] args) {
int abs = Math.abs(-9);
System.out.println(abs);
double pow = Math.pow(2, 4);
System.out.println(pow);
double ceil = Math.ceil(3.9);
System.out.println(ceil);
double floor = Math.floor(-4.999);
System.out.println(floor);
long round = Math.round(5.51);
System.out.println(round);
double sqrt = Math.sqrt(9.0);
System.out.println(sqrt);
int min = Math.min(1, 9);
int max = Math.max(45, 90);
System.out.println("min=" + min);
System.out.println("max=" + max);
for(int i = 0; i < 100; i++) {
System.out.println((int)(2 + Math.random() * (7 - 2 + 1)));
}
}
}
java.util.Random(随机数)
常用方法
方法名 | 功能 |
---|
Random() | 创建随机数对象 , 用来生成随机数 |
int nextInt() | 随机产生一个int类型取值范围内的数字 |
int nextInt(int a ) | 产生[0~a]之间的随机整数 , 不包含a |
生成5个不重复[0-100]的随机数并放到数组中
- 修改数组中元素的默认值 , 或者使用 Integer 类型的数组 , 防止生成的随机数和数组元素的默认值重复
public class RandomTest02 {
public static void main(String[] args) {
Random random = new Random();
int[] arr = new int[5];
for(int i = 0; i < arr.length; i++){
arr[i] = -1;
}
int index = 0;
while(index < arr.length){
int num = random.nextInt(101);
System.out.println("生成的随机数:" + num);
if(!contains(arr, num)){
arr[index++] = num;
}
}
for(int i = 0; i < arr.length; i++){
System.out.println(arr[i]);
}
}
public static boolean contains(int[] arr, int key){
for(int i = 0; i < arr.length; i++){
if(arr[i] == key){
return true;
}
}
return false;
}
}