java.math
Java中Math函数的使用
说到Java中的Math函数,大家肯定不陌生,但是在真正使用的时候却犯了难,那么多方法,我们到底需要使用哪个呢?
为此,我特地研究了一些Math常用函数的使用,以方便大家使用。
算术计算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Math.sqrt() : 计算平方根 Math.cbrt() : 计算立方根 Math.pow(a, b) : 计算a的b次方 Math.max( , ) : 计算最大值 Math.min( , ) : 计算最小值 Math.abs() : 取绝对值 System. out .println(Math.sqrt(16)); // 4.0 System. out .println(Math.cbrt(8)); // 2.0 System. out .println(Math.pow(3, 2)); // 9.0 System. out .println(Math.max(2.3, 4.5)); // 4.5 System. out .println(Math.min(2.3, 4.5)); // 2.3 /** * abs求绝对值 */ System. out .println(Math.abs(-10.4)); // 10.4 System. out .println(Math.abs(10.1)); // 10.1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 进位 Math.ceil(): 天花板的意思,就是逢余进一 Math.floor() : 地板的意思,就是逢余舍一 Math.rint(): 四舍五入,返回 double 值。注意.5的时候会取偶数 Math.round(): 四舍五入, float 时返回 int 值, double 时返回 long 值 /** * ceil天花板的意思,就是逢余进一 */ 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 System. out .println( "-------------------" ); /** * floor地板的意思,就是逢余舍一 */ 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 System. out .println( "-------------------" ); /** * rint 四舍五入,返回double值 注意.5的时候会取偶数 异常的尴尬=。= */ 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 System. out .println( "-------------------" ); /** * round 四舍五入,float时返回int值,double时返回long值 */ System. out .println(Math.round(10)); // 10 System. out .println(Math.round(10.1)); // 10 System. out .println(Math.round(10.7)); // 11 System. out .println(Math.round(10.5)); // 11 System. out .println(Math.round(10.51)); // 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 |
【注意】这里有一个非常需要注意的一点是:这里所有进位的方法的入参都要保证是float或者double类型,否则进位方法将毫无意义。例如如下我们经常犯的错误:
1 2 3 | int a = 1300, b = 1000; System. out .println(Math.ceil(a / b)); // 1 表达式A(错误使用) System. out .println(Math.ceil(a / ( float )b)); // 2 表达式B(正确使用) |
看上去表达式A和表达式B没有什么区别,可仔细分析可知:a / b = 1 ,而 a / (float)b = 1.3, 实际上表达式A的Math.ceil()根本起不了任何作用。
随机数
1 2 3 4 | Math.random(): 取得一个[0, 1)范围内的随机数 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类型的数 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!