java数字相关类
java数字类
- 整数 Short,Int,Long
- 浮点数 Float,Double
- 大整数 BigInteger(大整数),BigDecimal(大浮点数)
- 随机数类 Random
- 工具类 Math
java.math包
大数字类
大整数类BigInteger
——支持无限大的整数运算
示例代码:
import java.math.BigInteger;
public class BigIntegerTest {
public static void main(String[] args) {
BigInteger b1 = new BigInteger("123456789"); // 声明BigInteger对象
BigInteger b2 = new BigInteger("987654321"); // 声明BigInteger对象
System.out.println("b1: " + b1 + ", b2:" + b2);
System.out.println("加法操作:" + b2.add(b1)); // 加法操作
System.out.println("减法操作:" + b2.subtract(b1)); // 减法操作
System.out.println("乘法操作:" + b2.multiply(b1)); // 乘法操作
System.out.println("除法操作:" + b2.divide(b1)); // 除法操作
System.out.println("最大数:" + b2.max(b1)); // 求出最大数
System.out.println("最小数:" + b2.min(b1)); // 求出最小数
BigInteger result[] = b2.divideAndRemainder(b1); // 求出余数的除法操作
System.out.println("商是:" + result[0] + ";余数是:" + result[1]);
System.out.println("等价性是:" + b1.equals(b2));
int flag = b1.compareTo(b2);
if (flag == -1)
System.out.println("比较操作: b1<b2");
else if (flag == 0)
System.out.println("比较操作: b1==b2");
else
System.out.println("比较操作: b1>b2");
}
}
大浮点数BigDecimal
——支持无限大的小数运算
——注意精度和截断
示例代码:
import java.math.BigDecimal;
public class BigDecimalTest {
public static void main(String[] args) {
BigDecimal b1 = new BigDecimal("123456789.987654321"); // 声明BigDecimal对象
BigDecimal b2 = new BigDecimal("987654321.123456789"); // 声明BigDecimal对象
System.out.println("b1: " + b1 + ", b2:" + b2);
System.out.println("加法操作:" + b2.add(b1)); // 加法操作
System.out.println("减法操作:" + b2.subtract(b1)); // 减法操作
System.out.println("乘法操作:" + b2.multiply(b1)); // 乘法操作
//需要指定位数,防止无限循环,或者包含在try-catch中
System.out.println("除法操作:" + b2.divide(b1,10,BigDecimal.ROUND_HALF_UP)); // 除法操作
System.out.println("最大数:" + b2.max(b1)); // 求出最大数
System.out.println("最小数:" + b2.min(b1)); // 求出最小数
int flag = b1.compareTo(b2);
if (flag == -1)
System.out.println("比较操作: b1<b2");
else if (flag == 0)
System.out.println("比较操作: b1==b2");
else
System.out.println("比较操作: b1>b2");
System.out.println("===================");
//尽量采用字符串赋值
System.out.println(new BigDecimal("2.3"));
System.out.println(new BigDecimal(2.3));
System.out.println("===================");
BigDecimal num1 = new BigDecimal("10");
BigDecimal num2 = new BigDecimal("3");
//需要指定位数,防止无限循环,或者包含在try-catch中
BigDecimal num3 = num1.divide(num2, 3, BigDecimal.ROUND_HALF_UP);
System.out.println(num3);
}
}
随机数类
– nextInt() 返回一个随机
– nextInt(int a) 返回一个 [0,a) 之间的随机 int
– nextDouble() 返回一个 [0.0,1.0] 之间 double
– ints 方法批量返回随机数数组
——Math.random() 返回一个 [0.0,1.0] 之间 double
代码示例:
import java.util.Random;
public class RandomTest {
public static void main(String[] args)
{
//第一种办法,采用Random类 随机生成在int范围内的随机数
Random rd = new Random();
System.out.println(rd.nextInt());
System.out.println(rd.nextInt(100)); //0--100的随机数
System.out.println(rd.nextLong());
System.out.println(rd.nextDouble());
System.out.println("=========================");
//第二种,生成一个范围内的随机数 例如0到时10之间的随机数
//Math.random[0,1)
System.out.println(Math.round(Math.random()*10));
System.out.println("=========================");
//JDK 8 新增方法
rd.ints(); //返回无限个int类型范围内的数据
int[] arr = rd.ints(10).toArray(); //生成10个int范围类的个数。
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("=========================");
arr = rd.ints(5, 10, 100).toArray();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("=========================");
arr = rd.ints(10).limit(5).toArray();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
数字工具类
• java.lang.Math
– 绝对值函数 abs
– 对数函数 log
– 比较函数 max 、 min
– 幂函数 pow
– 四舍五入函数 round 等
– 向下取整 floor
– 向上取整
示例代码:
public class MathTest {
public static void main(String[] args) {
System.out.println(Math.abs(-5)); //绝对值
System.out.println(Math.max(-5,-8)); //最大值
System.out.println(Math.pow(-5,2)); //求幂
System.out.println(Math.round(3.5)); //四舍五入
System.out.println(Math.ceil(3.5)); //向上取整
System.out.println(Math.floor(3.5)); //向下取整
}
}