前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【蓝桥杯Java_C组·从零开始卷】第六节(一)、Java常用数学函数

【蓝桥杯Java_C组·从零开始卷】第六节(一)、Java常用数学函数

作者头像
红目香薰
发布2022-11-29 18:09:42
2220
发布2022-11-29 18:09:42
举报
文章被收录于专栏:CSDNToQQCode

目录

1.Math.sqrt() : 计算平方根

2.Math.cbrt() : 计算立方根

3.Math.pow(a, b) : 计算a的b次方

4.Math.max( , ) :计算最大值

5.Math.min( , ) : 计算最小值

6.Math.abs() : 取绝对值

7.Math.ceil(): 向上取整

8.Math.floor() : 向下取整

9.Math.rint(): 四舍五入,返回double值。注意.5的时候会取偶数

10.Math.round(): 四舍五入,float时返回int值,double时返回long值

11.Math.random(): 取得一个[0, 1)范围内的随机数

12.π

1.Math.sqrt() : 计算平方根

代码语言:javascript
复制
System.out.println(Math.sqrt(25)); // 5.0

2.Math.cbrt() : 计算立方根

代码语言:javascript
复制
System.out.println(Math.cbrt(27)); // 3.0 

3.Math.pow(a, b) : 计算a的b次方

代码语言:javascript
复制
System.out.println(Math.pow(3, 2)); // 9.0

4.Math.max( , ) :计算最大值

代码语言:javascript
复制
System.out.println(Math.max(2.3, 4.5));// 4.5

5.Math.min( , ) : 计算最小值

代码语言:javascript
复制
System.out.println(Math.min(2.3, 4.5));// 2.3

6.Math.abs() : 取绝对值

代码语言:javascript
复制
System.out.println(Math.abs(-10.4)); // 10.4
System.out.println(Math.abs(10.1)); // 10.1

7.Math.ceil(): 向上取整

代码语言:javascript
复制
System.out.println(Math.ceil(-10.1)); // -10.0
System.out.println(Math.ceil(10.7)); // 11.0
System.out.println(Math.ceil(-0.7)); // -0.0
System.out.println(Math.ceil(0.0)); // 0.0
System.out.println(Math.ceil(-0.0)); // -0.0
System.out.println(Math.ceil(-1.7)); // -1.0

8.Math.floor() : 向下取整

代码语言:javascript
复制
System.out.println(Math.floor(-10.1)); // -11.0
System.out.println(Math.floor(10.7)); // 10.0
System.out.println(Math.floor(-0.7)); // -1.0
System.out.println(Math.floor(0.0)); // 0.0
System.out.println(Math.floor(-0.0)); // -0.0

9.Math.rint(): 四舍五入,返回double值。注意.5的时候会取偶数

代码语言:javascript
复制
System.out.println(Math.rint(10.1)); // 10.0
System.out.println(Math.rint(10.7)); // 11.0
System.out.println(Math.rint(11.5)); // 12.0
System.out.println(Math.rint(10.5)); // 10.0
System.out.println(Math.rint(10.51)); // 11.0
System.out.println(Math.rint(-10.5)); // -10.0
System.out.println(Math.rint(-11.5)); // -12.0
System.out.println(Math.rint(-10.51)); // -11.0
System.out.println(Math.rint(-10.6)); // -11.0
System.out.println(Math.rint(-10.2)); // -10.0

10.Math.round(): 四舍五入,float时返回int值,double时返回long值

代码语言:javascript
复制
System.out.println(Math.round(10.1)); // 10
System.out.println(Math.round(10.5)); // 11
System.out.println(Math.round(-10.5)); // -10
System.out.println(Math.round(-10.51)); // -11
System.out.println(Math.round(-10.6)); // -11
System.out.println(Math.round(-10.2)); // -10

11.Math.random(): 取得一个[0, 1)范围内的随机数

代码语言:javascript
复制
System.out.println(Math.random()); // [0, 1)的double类型的数
System.out.println(Math.random() * 2);//[0, 2)的double类型的数
System.out.println(Math.random() * 2 + 1);// [1, 3)的double类型的数

12.π

代码语言:javascript
复制
System.out.println(Math.PI);//3.141592653589793
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-01-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.Math.sqrt() : 计算平方根
  • 2.Math.cbrt() : 计算立方根
  • 3.Math.pow(a, b) : 计算a的b次方
  • 4.Math.max( , ) :计算最大值
  • 5.Math.min( , ) : 计算最小值
  • 6.Math.abs() : 取绝对值
  • 7.Math.ceil(): 向上取整
  • 8.Math.floor() : 向下取整
  • 9.Math.rint(): 四舍五入,返回double值。注意.5的时候会取偶数
  • 10.Math.round(): 四舍五入,float时返回int值,double时返回long值
  • 11.Math.random(): 取得一个[0, 1)范围内的随机数
  • 12.π
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档