• Java Math常用方法


    1 Math.abs() 绝对值

    2 Math.max 最大值 

    3 Math.min 最小值 

    4  Math.round() 四舍五入取整

    4.1 保留几位小数

    5  Math.addExact() 整数之和

    6  Math.cbrt() 立方根

    7  Math.sqrt() 平方根

    8  Math.ceil() 向上取整

    9  Math.floor() 向下取整

    10  Math.random() 取随机数

    10.1  随机数

    11  Math.negateExact() 取反数

    package haoqipei.demo;
    
    import java.text.DecimalFormat;
    import java.util.Random;
    
    /**
     * @author :zhouqiang
     * @date :2021/6/29 15:42
     * @description:
     * @version: $
     */
    public class math {
        public static void main(String[] args) {
            /**  1
             * absolute value 绝对值
             * 值:1213.123
             */
            System.out.println(Math.abs(-1213.123));
    
            /** 2
             * 最大值
             * 值:2232
             */
            System.out.println(Math.max(112,2232));
    
            /** 3
             * 最小值
             * 值:112
             */
            System.out.println(Math.min(112,2232));
    
            /** 4
             * 四舍五入取整
             * 值:24
             */
            System.out.println(Math.round(23.75433));
    
            /** 4.1 保留小数
             * 示例:2位小数 和 4位小数
             * 值:23.75 和23.7544
             */
            DecimalFormat b = new DecimalFormat("0.00");
            String format = b.format(23.75433);
            System.out.println(format);
            DecimalFormat c = new DecimalFormat("0.0000");
            String format1 = c.format(23.75438823);
            System.out.println(format1);
    
    
            /** 5
             * 整数之和 Int 和long类型
             * 值:24
             */
            System.out.println(Math.addExact(-123,-23));
    
    
            /** 6
             *  立方根
             * 值:3.0
             */
            System.out.println(Math.cbrt(27));
    
            /** 7
             *  平方根
             * 值:4.0
             */
            System.out.println(Math.sqrt(16));
    
            /** 8
             *  向上取整
             * 值:11.0
             */
            System.out.println(Math.ceil(10.1));
    
            /** 9
             *  向下取整
             * 值:10.0
             */
            System.out.println(Math.floor(10.9));
    
            /** 10
             *  获取0-1随机数 或者 Math.random()*(最大值-最小值)+最小值
             *  示例 取 10-15 之间的随机数
             * 值:13.653823703277194
             */
            System.out.println(Math.random()*5+10);
    
            /** 10.1
             *  获取0-1随机数 或者 Math.random()*(最大值-最小值)+最小值
             *  示例 取 10-15 之间的随机数
             * 值:13.653823703277194
             */
            //以系统当前时间作为随机数生成的种子
            Random random=new Random();
            //返回一个大于0且小于100的整数
            System.out.println(random.nextInt(100));
            //返回一个随机浮点型
            System.out.println(random.nextFloat());
            //返回一个随机布尔型值
            System.out.println(random.nextBoolean());
            //返回一个随机双精度型
            System.out.println(random.nextDouble());
            //返回一个随机长整形
            System.out.println(random.nextLong());
    
    
            /** 11 取反值
             *
             * 值:-123
             */
            System.out.println(Math.negateExact(123));
    
            
    
        }
    }
  • 相关阅读:
    SAP ABAP 开发中常用的方法总结
    安装sap帮助文档
    spring成神之路第七篇:DI依赖注入之手动注入
    spring成神之路第三篇:Spring容器基本使用及原理(ApplicationContext接口获取bean的4种方式)
    spring成神之路第九篇:dependon到底是干什么的?
    spring成神之路第十篇:primary可以解决什么问题?
    spring成神之路第四篇:xml中bean定义详解
    spring成神之路第六篇:玩转bean scope,避免跳坑里
    spring成神之路第一篇:为何要学spring
    spring成神之路第五篇:创建bean实例的方式
  • 原文地址:https://www.cnblogs.com/zq1003/p/14950945.html
Copyright © 2020-2023  润新知