• Java基础知识回顾-18(Math类,Arrays类和大数据运算)


    public class MathDemo {
    
        public static void main(String[] args) {
            double x=12.2;
            //取绝对值
            System.out.println(Math.abs(-99));
            //向上取整
            System.out.println(Math.ceil(x));
            //向下取整
            System.out.println(Math.floor(x));
            //取最大值
            System.out.println(Math.max(2,5));
            //取最小值
            System.out.println(Math.min(2,5));
            //取次幂
            System.out.println(Math.pow(2,3));
            //取随机数[0,1)
            System.out.println(Math.random());
            //四舍五入
            System.out.println(Math.round(12.6));
            System.out.println(Math.round(12.5));
            System.out.println(Math.round(12.4));
            //π
            System.out.println(Math.PI);
        }
    import java.util.Arrays;
    
    public class ArraysDemo {
        public static void main(String[] args) {
            method2();
        }
        public static void method1(){
            int[] arr={3,6,9,11,15};
            //返回有序数组中,指定值所对应的索引值。
            //指定值如果不存在,索引值=(-当前索引-1)
            int i=Arrays.binarySearch(arr, 6);
            System.out.println(i);
        }
        
        public static void method2(){
            //将数组升序排列
            int[] arr={5,7,1,6,3,2,9};
            Arrays.sort(arr);
            System.out.println(Arrays.toString(arr));
            for(int a:arr){
                System.out.print(a+" ");
            }
        }
    }
    import java.math.BigInteger;

    public
    class BigDemo { public static void main(String[]args){ BigInteger b1=new BigInteger("10000000000000000000000000000000000"); BigInteger b2=new BigInteger("20000000000000000000000000000000000"); //加法 System.out.println(b1.add(b2)); //减法 System.out.println(b1.subtract(b2)); //乘法 System.out.println(b1.multiply(b2)); //除法 System.out.println(b1.divide(b2)); } }
    import java.math.BigDecimal;
    
    public class BigDecimalDemo {
    
    	public static void main(String[] args) {
    		 method1();
    		 method2();
    		 method3();
    		 method4();
    
    	}
    	public static void method1(){
    		//为避免损失精度用BigDecimal计算加法
    		BigDecimal bd1=new BigDecimal("0.09");
    		BigDecimal bd2=new BigDecimal("0.01");
    		System.out.println(bd1.add(bd2));
    	}
    	public static void method2(){
    		//为避免损失精度用BigDecimal计算减法
    		BigDecimal bd1=new BigDecimal("1.0");
    		BigDecimal bd2=new BigDecimal("0.32");
    		System.out.println(bd1.subtract(bd2));
    	}
    	public static void method3(){
    		//为避免损失精度用BigDecimal计算乘法
    		BigDecimal bd1=new BigDecimal("1.02");
    		BigDecimal bd2=new BigDecimal("0.32");
    		System.out.println(bd1.multiply(bd2));
    	}
    	public static void method4(){
    		//为避免损失精度用BigDecimal计算除法
    		BigDecimal bd1=new BigDecimal("1.0");
    		BigDecimal bd2=new BigDecimal("0.3");
    		System.out.println(bd1.divide(bd2,BigDecimal.ROUND_FLOOR,3));
    	}
    
    }
    
  • 相关阅读:
    .NetCore中获取持久化哈希值
    switch类型模式
    .NET6之MiniAPI(三):Response
    .NET6之MiniAPI(二):request
    .NET6之MiniAPI(一):开始Mini API
    .net6给winform带来的新功能
    2021.NET Conf China上的GraphQL
    Maptalks 注册自定义绘图模块
    MinIO服务器搭建(单机版)
    C++ mysqlclient create and insert into tables
  • 原文地址:https://www.cnblogs.com/zhangrui0328/p/9094069.html
Copyright © 2020-2023  润新知