• Math类 BigInteger 类 BigDecimal类 基础用法


    1.   Math类概念

    Math 类是包含用于执行基本数学运算的方法的数学工具类,其所有方法均为静态方法。

    类方法:

    复制代码
    public class Demo01 {
        public static void main(String[] args) {
            //求绝对值
            System.out.println(Math.abs(-2.8));
            //向上取整
            System.out.println(Math.ceil(12.2));
            //向下取整
            System.out.println(Math.floor(12.8));
            //求两个值的最大值
            System.out.println(Math.max(12.3, 89));
            //求两个值的最小值
            System.out.println(Math.min(54, 12));
            //求随机数
            System.out.println(Math.random());
            //四舍五入
            System.out.println(Math.round(45.3));
            //次幂
            System.out.println(Math.pow(2, 8));
        }
    }
    复制代码

    2  Arrays类

    复制代码
    public class Demo02 {
        public static void main(String[] args) {
            //数组排序
            int []arr={2,5,2,6,8,9};
            Arrays.sort(arr);
            for(int i=0;i<arr.length;i++){
                System.out.print(arr[i]+" ");
            }
            System.out.println();
            //在数组里查找指定值的索引值
            //前提:数组必须有序
            //如果查询的值在数组中不存在,index=-(这个值应该在的位置)-1
            int[]a2={1,3,55,66,99};
            int index=Arrays.binarySearch(a2, 66);
            System.out.println(index);
            //将int[]——>String
            System.out.println(Arrays.toString(a2));
        }
    }
    复制代码

    3     BigInteger大数据

     java中long型为最大整数类型,对于超过long型的数据如何去表示呢.在Java的世界中,超过long型的整数已经不能被称为整数了,它们被封装成BigInteger对象.在BigInteger类中,实现四则运算都是方法来实现,并不是采用运算符.

    复制代码
    public class Demo03 {
        public static void main(String[] args) {
            BigInteger b1=new BigInteger("1000000000000000000000000000000000000");
            BigInteger b2=new BigInteger("9000000000000000000000000000000000000");
            //加法
            System.out.println(b1.add(b2));
            //减法
            System.out.println(b2.subtract(b1));
            //乘法
            System.out.println(b1.multiply(b2));
            //除法
            System.out.println(b2.divide(b1));
        }
    }
    复制代码

    4     BigDecimal类

    double和float类型在运算中很容易丢失精度,造成数据的不准确性,Java提供我们BigDecimal类可以实现浮点数据的高精度运算

    复制代码
    public class Demo04 {
        public static void main(String[] args) {
            BigDecimal b1=new BigDecimal("0.09");
            BigDecimal b2=new BigDecimal("0.01");
            //加法
            System.out.println(b1.add(b2));
            //减法
            BigDecimal b3=new BigDecimal("1.0");
            BigDecimal b4=new BigDecimal("0.32");
            System.out.println(b3.subtract(b4));
            //乘法
            BigDecimal b5=new BigDecimal("1.015");
            BigDecimal b6=new BigDecimal("100");
            System.out.println(b5.multiply(b6));
            //除法
            BigDecimal b7=new BigDecimal("1.301");
            BigDecimal b8=new BigDecimal("100");
            System.out.println(b7.divide(b8));
            //向上取整并保留两位小数
            System.out.println(b7.divide(b8,2,BigDecimal.ROUND_CEILING));
        }
    }
    复制代码
  • 相关阅读:
    WC2020「Illusory」
    WC2015-2019
    Java基础——数组
    Java基础——Java基础语法和使用
    完善README——MarkDown模板[EN]
    完善README——MarkDown模板[CN]
    完善README——Markdown语法概要总结
    Java基础——Java如何导入本地项目
    Java基础——Java的历史以及平台应用
    AndroidSDK——和风天气使用初体验
  • 原文地址:https://www.cnblogs.com/lxzwhite/p/10701161.html
Copyright © 2020-2023  润新知