zoukankan      html  css  js  c++  java
  • Java-Math

    java.lang.Math类提供的方法都是static的,“静态引入 ”使得不必每次在调用类方法时都在方法前写上类名:             import static java.lang.Math.*;
    这样在调用Math的方法时就可以简单地写出方法名,例如:             cos(radians);
    ----------------------------------------------------------
    1、基本方法: abs, max, min, ceil, floor, rint, round

    重载abs方法,返回一个数(int、long、float、double)的绝对值

     

       Math.abs(-30.5)     == 30.5

       Math.abs(-100.0989)    == 100.0989

     

      Math.ceil(30.4)         == 31.0
       Math.ceil(-8.0989)   == -8.0

    重载max和min方法,返回两个数(int、long、float、double)的最大值和最小值

    public static double ceil(double x);  //向上取整,返回double

    public static double floor(double x);  //向下取整,返回double

    public static double rint(double x);  //以double值返回与x最接近的整数,如果x到两个整数的距离相等,返回其中的偶数

    public static long round(double x);  //返回(long)Math.floor(x+0.5);

    public static int round(float x);  //返回(int)Math.floor(x+0.5);

    2、指数和对数方法: (Math.E = 2.7183) exp, log, pow, sqrt
    3、三角函数: sin, cos, tan, asin, acos, atan double atan2 (double y, double x);  //将直角坐标系的坐标(x, y)转变为极坐标中的坐标(r, theta),并返回角度thera public static double toDegrees(double radians);

    public static double toRadians(double degree);
    4、随机数 0.0 <= Math.random() < 1.0 如果要得到一个[0, 10)之间的随机整数: int number = (int)(Math.random() * 10);

    如果要得到一个[50, 100)之间的随机整数:

    int number = 50 + (int)(Math.random() * 50);

    如果要得到一个(a, a+b]之间的随机整数:

    int number = a + (int)(Math.random() * b);

    使用Math.Random()可以得到单个随机数,但若要得到一系列随机数,则可以使用java.util.Random 类,通过创建对象,调用相应的方法实现

  • 相关阅读:
    springboot+vue实现前后端分离之前端vue部分(spring boot 2.5.4/vue.js 3.2.4)
    如何给一个vue项目重命名(vue.js 3.2.4)
    用git命令上传一个项目到gitee(git 2.30.2)
    kde plasma 5.21:为应用程序添加桌面快捷方式(kubuntu 21.04)
    @vue/cli 4.5.13:创建一个vue.js3.x项目(vue.js 3.2.4)
    linux:ubuntu21.04:npm安装@vue/cli时报错(@vue/cli 4.5.13/npm 7.21.0/node 14.17.1)
    python 装饰器模式
    staticmethod classmethod property
    presto 获取hive 表最大分区
    ALIGN(v, a)
  • 原文地址:https://www.cnblogs.com/plxx/p/3173013.html
Copyright © 2011-2022 走看看