栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 数字相关类 大整数类BigInteger 大浮点数BigDecimal 随机数类 数字工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 数字相关类 大整数类BigInteger 大浮点数BigDecimal 随机数类 数字工具类

数字相关类

大整数类BigInteger
public class BigIntegerTest {
    public static void main(String[] args) {
        BigInteger b1 = new BigInteger("123456789");
        BigInteger b2 = new BigInteger("987654321");
        System.out.println(b1);
        System.out.println(b2);
        System.out.println("加法操作:" + b1.add(b2));
        System.out.println("减法操作:" + b1.subtract(b2));
        System.out.println("乘法操作:" + b1.multiply(b2));
 
        //取整
        System.out.println("除法操作:" + b2.divide(b1));
        System.out.println("最大数:" + b1.max(b2));
        System.out.println("最小值:" + b1.min(b2));
 
        //计算除法的商和余数
        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("比较操作:b1b2");
        }
    }
}

大浮点数BigDecimal
public class BigDecimalTest {
    public static void main(String[] args) {
        //注:尽量使用字符串赋值,防止出现浮点精度问题。
 
        BigDecimal b1 = new BigDecimal("123456789.987654321");
        BigDecimal b2 = new BigDecimal("987654321.123456789");
        System.out.println(b1);
        System.out.println(b2);
        System.out.println("加法操作:" + b1.add(b2));
        System.out.println("减法操作:" + b1.subtract(b2));
        System.out.println("乘法操作:" + b1.multiply(b2));
 
        //除法操作需要指定位数,防止无限循环。或者放到try-catch中
        System.out.println("除法操作:" + b2.divide(b1, 10, RoundingMode.HALF_UP));
        System.out.println("最大数:" + b1.max(b2));
        System.out.println("最小数:" + b1.min(b2));
        System.out.println("等价性是:" + b1.equals(b2));
 
        //比较大小
        int flag = b1.compareTo(b2);
        if (flag == -1) {
            System.out.println("比较操作:b1b2");
        }
    }
}

随机数类
public class RandomTest {
    public static void main(String[] args) {
        
        Random random = new Random();
        //返回一个int类型的随机数
        System.out.println(random.nextInt());
        //返回一个[0, 100)范围内int型的随机数
        System.out.println(random.nextInt(100));
        //返回一个Long类型的随机数
        System.out.println(random.nextLong());
        //返回一个(0,1)之间Double类型的随机数
        System.out.println(random.nextDouble());
        //返回多个int类型的随机数
        int[] arr = random.ints(10).toArray();
        for (int a : arr) {
            System.out.println(a);
        }
        //返回多个指定范围内的随机数
        arr = random.ints(10, 1, 100).toArray();
        for (int a : arr) {
            System.out.println(a);
        }
 
        
        //返回一个[0, 10)之间Double类型的随机数
        System.out.println(Math.round(Math.random() * 10));
 
    }
}

数字工具类
public class MathTest {
    public static void main(String[] args) {
        //绝对值| |  1
        System.out.println(Math.abs(-1));
        //比较函数max min  2  1
        System.out.println(Math.max(1, 2));
        System.out.println(Math.min(1, 2));
        //幂函数pow  等价于(-5)^2  25.0
        System.out.println(Math.pow(-5, 2));
        //四舍五入函数round  4
        System.out.println(Math.round(3.6));
        //向下取整floor  3
        System.out.println(Math.floor(3.6));
        //向上取整ceil  4
        System.out.println(Math.ceil(3.2));
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/345118.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号