java数学工具类Math详解(round方法)

作者:阿福97 时间:2022-09-10 17:01:00 

数学工具类Math,供大家参考,具体内容如下

1. 概述

java.util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作。

2. 基本的方法


public static double abs(double num);获取绝对值。有多种重载,absolutely绝对地
public static double ceil(double num);向上取整,ceil是天花板的意思
public static double floor(double num);向下取整,floor是地板的意思
public static long round(double num);四舍六入五成双(看下面代码的注释),round有大约,完整的意思

3. 四种方法一起通过代码演示一遍


public class MathMethod {
 public static void main(String[] args) {
   //abs方法,取绝对值
   System.out.println(Math.abs(3.14)); //3.14
   System.out.println(Math.abs(0));  //0
   System.out.println(Math.abs(-2.2)); //2.2

System.out.println("---------------------");

//ceil方法,向上取整,往大的靠
   System.out.println(Math.ceil(3.2)); //4.0
   System.out.println(Math.ceil(3.8)); //4.0
   System.out.println(Math.ceil(-3.2)); //-3.0
   System.out.println(Math.ceil(-3.8)); //-3.0

System.out.println("---------------------");

//floor方法,向下取整,往小的靠
   System.out.println(Math.floor(3.2)); //3.0
   System.out.println(Math.floor(3.8)); //3.0
   System.out.println(Math.floor(-3.2)); //-4.0
   System.out.println(Math.floor(-3.8)); //-4.0

System.out.println("---------------------");

//【注意,面试高频】round方法,四舍 六入 五成双
   //先看看四舍六入,如果出现负数,先转成正数,再四舍六入,最后加上负号
   System.out.println(Math.round(3.4)); //3
   System.out.println(Math.round(3.6)); //4
   System.out.println(Math.round(-3.4)); //-3
   System.out.println(Math.round(-3.6)); //-4
   //五成双是什么意思呢?当出现0.5结尾的时候,就给它再加上+0.5,5不就成双了
   //接着再对相加的结果进行floor运算
   System.out.println(Math.round(-2.5)); //-2
   System.out.println(Math.floor(-2.5 + 0.5)); //与Math.round(-2.5)结果一致

System.out.println(Math.round(2.5)); //3
   System.out.println(Math.floor(2.5 + 0.5)); //与Math.round(2.5)结果一致
 }
}

4. 圆周率Math.PI

在Math类的源码中,我们可以看到,它自定义的圆周率 PI = 3.14159265358979323846

以后的计算如果需要用到PI,尽量用已经定义好的圆周率,非常精确

来源:https://blog.csdn.net/weixin_43836046/article/details/96893054

标签:java,数学工具类,Math
0
投稿

猜你喜欢

  • Java线程池Executor用法详解

    2022-02-13 01:21:16
  • IDEA 2020.2 +Gradle 6.6.1 + Spring Boot 2.3.4 创建多模块项目的超详细教程

    2021-11-08 00:42:36
  • 浅谈java类和对象

    2021-10-01 06:01:59
  • Android刮刮卡效果实现代码

    2023-08-24 01:29:58
  • Java集合教程之Collection实例详解

    2022-12-01 23:45:13
  • 使用Spring Boot 2.x构建Web服务的详细代码

    2022-09-17 04:08:40
  • java并发JUC工具包AtomicInteger原子整型语法基础

    2023-10-05 14:16:47
  • Flutter实现底部导航栏创建详解

    2023-09-29 10:01:04
  • java 垃圾回收机制以及经典垃圾回收器详解

    2022-07-06 05:16:08
  • java多线程模拟抢红包功能

    2023-07-25 01:09:58
  • Java实现FTP上传与下载功能

    2021-09-22 18:28:51
  • asp.net实现遍历Request的信息操作示例

    2022-11-15 23:15:18
  • C#实现FFT(递归法)的示例代码

    2022-12-30 05:21:06
  • Java中final关键字的用法总结

    2023-01-06 19:47:48
  • Unity3D绘制地形的实现方法

    2022-12-01 01:06:46
  • Kotlin协程flowOn与线程切换超详细示例介绍

    2022-11-06 08:31:51
  • Android8.0适配前台定位服务service的示例代码

    2021-05-25 17:18:36
  • hutool实战:IoUtil 流操作工具类(将内容写到流中)

    2022-11-16 09:17:47
  • 浅谈Spring Cloud Ribbon的原理

    2023-07-23 04:11:25
  • Android WebView实现顶部进度条

    2023-10-14 23:44:03
  • asp之家 软件编程 m.aspxhome.com