zoukankan      html  css  js  c++  java
  • 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));
    
            
    
        }
    }
  • 相关阅读:
    Linux下CVS的配置
    MSVC中的"pseudo register"调试技术
    flash cs4专业版下载(有注册码)
    Away3d学习笔记 away3d类库下载
    Away3D实例教程 贴图(EnviroBitmapMaterial)
    Away3D实例教程 贴图(Dot3BitmapMaterial)
    Away3D学习笔记1 戏说Flash 三维引擎
    Away3d基础6 – 位图材质
    away3D基础6颜色材质
    Away3d实例教程 贴图(texture mapping)
  • 原文地址:https://www.cnblogs.com/zq1003/p/14950945.html
Copyright © 2011-2022 走看看