Math类常用方法大全
一.简介
Java的Math类封装了很多与数学有关的属性和方法。
二.举例说明
代码
private void mathMethod(){// Math.sqrt()//计算平方根 Math.cbrt()//计算立方根 Math.hypot(x,y)//计算 (x的平方+y的平方)的平方根Log.d("TAG","Math.sqrt(16)----:"+Math.sqrt(16));//4.0
Log.d("TAG","Math.cbrt(8)----:"+Math.cbrt(8));//2.0
Log.d("TAG","Math.hypot(3,4)----:"+Math.hypot(3,4));//5.0// Math.pow(a,b)//计算a的b次方 Math.exp(x)//计算e^x的值Log.d("TAG","------------------------------------------");
Log.d("TAG","Math.pow(3,2)----:"+Math.pow(3,2));//9.0
Log.d("TAG","Math.exp(3)----:"+Math.exp(3));//20.085536923187668//Math.max();//计算最大值 Math.min();//计算最小值Log.d("TAG","------------------------------------------");
Log.d("TAG","Math.max(2.3,4.5)----:"+Math.max(7,15));//15
Log.d("TAG","Math.min(2.3,4.5)----:"+Math.min(2.3,4.5));//2.3//Math.abs求绝对值Log.d("TAG","------------------------------------------");
Log.d("TAG","Math.abs(-10.4)----:"+Math.abs(-10.4));//10.4
Log.d("TAG","Math.abs(10.1)----:"+Math.abs(10.1));//10.1//Math.ceil天花板的意思,就是返回大的值Log.d("TAG","------------------------------------------");
Log.d("TAG","Math.ceil(-10.1)----:"+Math.ceil(-10.1));//-10.0
Log.d("TAG","Math.ceil(10.7)----:"+Math.ceil(10.7));//11.0
Log.d("TAG","Math.ceil(-0.7)----:"+Math.ceil(-0.7));//-0.0
Log.d("TAG","Math.ceil(0.0)----:"+Math.ceil(0.0));//0.0
Log.d("TAG","Math.ceil(-0.0)----:"+Math.ceil(-0.0));//-0.0
Log.d("TAG","Math.ceil(-1.7)----:"+Math.ceil(-1.7));//-1.0//Math.floor地板的意思,就是返回小的值Log.d("TAG","------------------------------------------");
Log.d("TAG","Math.floor(-10.1)----:"+Math.floor(-10.1));//-11.0
Log.d("TAG","Math.floor(10.7)----:"+Math.floor(10.7));//10.0
Log.d("TAG","Math.floor(-0.7)----:"+Math.floor(-0.7));//-1.0
Log.d("TAG","Math.floor(0.0)----:"+Math.floor(0.0));//0.0
Log.d("TAG","Math.floor(-0.0)----:"+Math.floor(-0.0));//-0.0//Math.random 取得一个大于或者等于0.0小于不等于1.0的随机数[0,1)Log.d("TAG","------------------------------------------");
Log.d("TAG","Math.random()----:"+Math.random());//输出[0,1)间的随机数 0.8979626325354049
Log.d("TAG","Math.random()*100----:"+Math.random()*100);//输出[0,100)间的随机数 32.783762836248144// Math.rint 四舍五入 返回double值Log.d("TAG","------------------------------------------");
Log.d("TAG","Math.rint(10.1)----:"+Math.rint(10.1));//10.0
Log.d("TAG","Math.rint(10.7)----:"+Math.rint(10.7));//11.0
Log.d("TAG","Math.rint(-10.5)----:"+Math.rint(-10.5));//-10.0
Log.d("TAG","Math.rint(-10.51)----:"+Math.rint(-10.51));//-11.0
Log.d("TAG","Math.rint(-10.2)----:"+Math.rint(-10.2));//-10.0
Log.d("TAG","Math.rint(9)----:"+Math.rint(9));//9.0//Math.round 四舍五入 float时返回int值,double时返回long值Log.d("TAG","------------------------------------------");
Log.d("TAG","Math.round(10.1)----:"+Math.round(10.1));//10
Log.d("TAG","Math.round(10.7)----:"+Math.round(10.7));//11
Log.d("TAG","Math.round(-10.5)----:"+Math.round(-10.5));//-10
Log.d("TAG","Math.round(-10.51)----:"+Math.round(-10.51));//-11
Log.d("TAG","Math.round(-10.2)----:"+Math.round(-10.2));//-10
Log.d("TAG","Math.round(9)----:"+Math.round(9));//9//Math.nextUp(a) 返回比a大一点点的浮点数Log.d("TAG","------------------------------------------");
Log.d("TAG","Math.nextUp(1.2)----:"+Math.nextUp(1.2));//1.2000000000000002//Math.nextDown(a) 返回比a小一点点的浮点数Log.d("TAG","------------------------------------------");
Log.d("TAG","Math.nextDown(1.2)----:"+Math.nextDown(1.2));//1.1999999999999997//Math.nextAfter(a,b) 返回(a,b)或(b,a)间与a相邻的浮点数 b可以比a小Log.d("TAG","------------------------------------------");
Log.d("TAG","Math.nextAfter(1.2, 2.7)----:"+Math.nextAfter(1.2, 2.7));//1.2000000000000002
Log.d("TAG","Math.nextAfter(1.2, -1)----:"+Math.nextAfter(1.2, -1));//1.1999999999999997}
结果
Math.sqrt(16)----:4.0
Math.cbrt(8)----:2.0
Math.hypot(3,4)----:5.0
------------------------------------------
Math.pow(3,2)----:9.0
Math.exp(3)----:20.085536923187668
------------------------------------------
Math.max(2.3,4.5)----:15
Math.min(2.3,4.5)----:2.3
------------------------------------------
Math.abs(-10.4)----:10.4
Math.abs(10.1)----:10.1
------------------------------------------
Math.ceil(-10.1)----:-10.0
Math.ceil(10.7)----:11.0
Math.ceil(-0.7)----:-0.0
Math.ceil(0.0)----:0.0
Math.ceil(-0.0)----:-0.0
Math.ceil(-1.7)----:-1.0
------------------------------------------
Math.floor(-10.1)----:-11.0
Math.floor(10.7)----:10.0
Math.floor(-0.7)----:-1.0
Math.floor(0.0)----:0.0
Math.floor(-0.0)----:-0.0
------------------------------------------
Math.random()----:0.8979626325354049
Math.random()*100----:32.783762836248144
------------------------------------------
Math.rint(10.1)----:10.0
Math.rint(10.7)----:11.0
Math.rint(-10.5)----:-10.0
Math.rint(-10.51)----:-11.0
Math.rint(-10.2)----:-10.0
Math.rint(9)----:9.0
------------------------------------------
Math.round(10.1)----:10
Math.round(10.7)----:11
Math.round(-10.5)----:-10
Math.round(-10.51)----:-11
Math.round(-10.2)----:-10
Math.round(9)----:9
------------------------------------------
Math.nextUp(1.2)----:1.2000000000000002
Math.nextDown(1.2)----:1.1999999999999997
Math.nextAfter(1.2, 2.7)----:1.2000000000000002
Math.nextAfter(1.2, -1)----:1.1999999999999997
Math类常用方法大全相关推荐
- java中math的方法_Java中Math类常用方法代码详解
近期用到四舍五入想到以前整理了一点,就顺便重新整理好经常见到的一些四舍五入,后续遇到常用也会直接在这篇文章更新... public class Demo{ public static void mai ...
- Java 之 Math类 常用方法详解
Math类是数学工具,它是一个final类 ,不能被继承,也不能被创建对象.它提供的所有属性和方法都是static修饰 ,直接使用类名.方法名/属性来调用.接下来就介绍常用的Math类的方法~ 目录 ...
- Class -- 10 -- Method类常用方法解析
原文链接:Class – 10 – Method类常用方法解析 相关文章: Class – 01 – System类常用方法解析 Class – 02 – Arrays类常用方法解析 Class – ...
- Class -- 08 -- Parameter类常用方法解析
原文链接:Class – 08 – Parameter类常用方法解析 相关文章: Class – 01 – System类常用方法解析 Class – 02 – Arrays类常用方法解析 Class ...
- Class -- 03 -- Random类常用方法详解析
原文链接:Class – 03 – Random类常用方法详解析 相关文章: Class – 01 – System类常用方法解析 Class – 02 – Arrays类常用方法解析 Class – ...
- java math四舍五入类,Java中Math类的几个四舍五入方法的区别
JAVA取整以及四舍五入 下面来介绍将小数值舍入为整数的几个方法:Math.ceil().Math.floor()和Math.round(). 这三个方法分别遵循下列舍入规则: Math.ceil() ...
- Class -- 09 -- Field类常用方法解析
原文链接:Class – 09 – Field类常用方法解析 相关文章: Class – 01 – System类常用方法解析 Class – 02 – Arrays类常用方法解析 Class – 0 ...
- java中立方根方法_java基础:4、java中Math类的常用方法?
4.java中Math类的常用方法? Java的Math类封装了很多与数学有关的属性和方法.如下所示: System.out.println("计算平方根--Math.sqrt(81)-- ...
- [Java基础]Math类的常用方法
Math类的常用方法:
- java 求整_Java Math类的常用方法,求整运算
你知道Java Math类的常用方法求整运算应该如何实现吗?下面的文章要给大家介绍到的就是这个方面的内容,一起来了解了解吧. Math 类的求整方法有很多,详细说明如下所示: 下面的实例演示了 Mat ...
最新文章
- 互联网1分钟 |1026
- NOIP2017年11月9日赛前模拟
- opencv24-直方图比较
- java最全基础知识_Java编程入门,选择排序(Selection Sort)怎么做?
- Python内置函数(66)——vars
- responseentity 详解_如何正确选购和使用电动自行车?7个问答详解!_政务_澎湃新闻...
- AXURE RP 原型图绘制手册
- Kossel 升级记 - 混乱之始
- 2023系统分析师综合知识必备知识点
- php Allowed memory size of 134217728 bytes exhausted
- 什么是Harmony操作系统?华为新操作系统介绍
- python-matplotlib 绘制函数曲线
- pymysql 向MySQL 插入数据无故报错
- testin云测操作步骤
- 2022年全国最新消防设施操作员(初级消防设施操作员)题库及答案
- 在 ubuntu20.04下搭建 lamp 环境并制作静态网页
- SpringBoot国际化失败的原因,切换中英文无效
- 联考素描头像怎么才能画到高分?联考新手必看!
- svn 项目文件出现左边红色箭头,右边绿色箭头的双箭头的解决方法
- android8.0大写英文字母,Android EditTextView 设置输入英文字母全部大写