Java Math类常用方法整理







1.简介

Java的Math类封装了很多与数学有关的属性和方法。



2.举例说明


代码:


    
    
  1. private void mathMethod(){
  2. /
  3. * Math.sqrt()//计算平方根
  4. * Math.cbrt()//计算立方根
  5. * Math.hypot(x,y)//计算 (x的平方+y的平方)的平方根
  6. /
  7. Log.d( "TAG", "Math.sqrt(16)----:"+Math.sqrt( 16)); //4.0
  8. Log.d( "TAG", "Math.cbrt(8)----:"+Math.cbrt( 8)); //2.0
  9. Log.d( "TAG", "Math.hypot(3,4)----:"+Math.hypot( 3, 4)); //5.0
  10. /
  11. Math.pow(a,b)//计算a的b次方
  12. * Math.exp(x)//计算e^x的值
  13. * /
  14. Log.d( "TAG", "------------------------------------------");
  15. Log.d( "TAG", "Math.pow(3,2)----:"+Math.pow( 3, 2)); //9.0
  16. Log.d( "TAG", "Math.exp(3)----:"+Math.exp( 3)); //20.085536923187668
  17. /
  18. Math.max();//计算最大值
  19. * Math.min();//计算最小值
  20. * /
  21. Log.d( "TAG", "------------------------------------------");
  22. Log.d( "TAG", "Math.max(2.3,4.5)----:"+Math.max( 7, 15)); //15
  23. Log.d( "TAG", "Math.min(2.3,4.5)----:"+Math.min( 2.3, 4.5)); //2.3
  24. /
  25. Math.abs求绝对值
  26. /
  27. Log.d( "TAG", "------------------------------------------");
  28. Log.d( "TAG", "Math.abs(-10.4)----:"+Math.abs(- 10.4)); //10.4
  29. Log.d( "TAG", "Math.abs(10.1)----:"+Math.abs( 10.1)); //10.1
  30. /
  31. Math.ceil天花板的意思,就是返回大的值
  32. /
  33. Log.d( "TAG", "------------------------------------------");
  34. Log.d( "TAG", "Math.ceil(-10.1)----:"+Math.ceil(- 10.1)); //-10.0
  35. Log.d( "TAG", "Math.ceil(10.7)----:"+Math.ceil( 10.7)); //11.0
  36. Log.d( "TAG", "Math.ceil(-0.7)----:"+Math.ceil(- 0.7)); //-0.0
  37. Log.d( "TAG", "Math.ceil(0.0)----:"+Math.ceil( 0.0)); //0.0
  38. Log.d( "TAG", "Math.ceil(-0.0)----:"+Math.ceil(- 0.0)); //-0.0
  39. Log.d( "TAG", "Math.ceil(-1.7)----:"+Math.ceil(- 1.7)); //-1.0
  40. /
  41. Math.floor地板的意思,就是返回小的值
  42. /
  43. Log.d( "TAG", "------------------------------------------");
  44. Log.d( "TAG", "Math.floor(-10.1)----:"+Math.floor(- 10.1)); //-11.0
  45. Log.d( "TAG", "Math.floor(10.7)----:"+Math.floor( 10.7)); //10.0
  46. Log.d( "TAG", "Math.floor(-0.7)----:"+Math.floor(- 0.7)); //-1.0
  47. Log.d( "TAG", "Math.floor(0.0)----:"+Math.floor( 0.0)); //0.0
  48. Log.d( "TAG", "Math.floor(-0.0)----:"+Math.floor(- 0.0)); //-0.0
  49. /
  50. Math.random 取得一个大于或者等于0.0小于不等于1.0的随机数[0,1)
  51. /
  52. Log.d( "TAG", "------------------------------------------");
  53. Log.d( "TAG", "Math.random()----:"+Math.random()); //输出[0,1)间的随机数 0.8979626325354049
  54. Log.d( "TAG", "Math.random()*100----:"+Math.random() 100); //输出[0,100)间的随机数 32.783762836248144
  55. /
  56. * Math.rint 四舍五入
  57. * 返回double值
  58. /
  59. Log.d( "TAG", "------------------------------------------");
  60. Log.d( "TAG", "Math.rint(10.1)----:"+Math.rint( 10.1)); //10.0
  61. Log.d( "TAG", "Math.rint(10.7)----:"+Math.rint( 10.7)); //11.0
  62. Log.d( "TAG", "Math.rint(-10.5)----:"+Math.rint(- 10.5)); //-10.0
  63. Log.d( "TAG", "Math.rint(-10.51)----:"+Math.rint(- 10.51)); //-11.0
  64. Log.d( "TAG", "Math.rint(-10.2)----:"+Math.rint(- 10.2)); //-10.0
  65. Log.d( "TAG", "Math.rint(9)----:"+Math.rint( 9)); //9.0
  66. /
  67. Math.round 四舍五入
  68. * float时返回int值,double时返回long值
  69. /
  70. Log.d( "TAG", "------------------------------------------");
  71. Log.d( "TAG", "Math.round(10.1)----:"+Math.round( 10.1)); //10
  72. Log.d( "TAG", "Math.round(10.7)----:"+Math.round( 10.7)); //11
  73. Log.d( "TAG", "Math.round(-10.5)----:"+Math.round(- 10.5)); //-10
  74. Log.d( "TAG", "Math.round(-10.51)----:"+Math.round(- 10.51)); //-11
  75. Log.d( "TAG", "Math.round(-10.2)----:"+Math.round(- 10.2)); //-10
  76. Log.d( "TAG", "Math.round(9)----:"+Math.round( 9)); //9
  77. /
  78. Math.nextUp(a) 返回比a大一点点的浮点数
  79. * Math.nextDown(a) 返回比a小一点点的浮点数
  80. * Math.nextAfter(a,b) 返回(a,b)或(b,a)间与a相邻的浮点数 b可以比a小
  81. * /
  82. Log.d( "TAG", "------------------------------------------");
  83. Log.d( "TAG", "Math.nextUp(1.2)----:"+Math.nextUp( 1.2)); //1.2000000000000002
  84. Log.d( "TAG", "Math.nextDown(1.2)----:"+Math.nextDown( 1.2)); //1.1999999999999997
  85. Log.d( "TAG", "Math.nextAfter(1.2, 2.7)----:"+Math.nextAfter( 1.2, 2.7)); //1.2000000000000002
  86. Log.d( "TAG", "Math.nextAfter(1.2, -1)----:"+Math.nextAfter( 1.2, - 1)); //1.1999999999999997
  87. }



结果:


    
    
  1. Math.sqrt( 16)----: 4.0
  2. Math.cbrt( 8)----: 2.0
  3. Math.hypot( 3, 4)----: 5.0
  4. ------------------------------------------
  5. Math.pow( 3, 2)----: 9.0
  6. Math.exp( 3)----: 20.085536923187668
  7. ------------------------------------------
  8. Math.max( 2.3, 4.5)----: 15
  9. Math.min( 2.3, 4.5)----: 2.3
  10. ------------------------------------------
  11. Math.abs(- 10.4)----: 10.4
  12. Math.abs( 10.1)----: 10.1
  13. ------------------------------------------
  14. Math.ceil(- 10.1)----:- 10.0
  15. Math.ceil( 10.7)----: 11.0
  16. Math.ceil(- 0.7)----:- 0.0
  17. Math.ceil( 0.0)----: 0.0
  18. Math.ceil(- 0.0)----:- 0.0
  19. Math.ceil(- 1.7)----:- 1.0
  20. ------------------------------------------
  21. Math.floor(- 10.1)----:- 11.0
  22. Math.floor( 10.7)----: 10.0
  23. Math.floor(- 0.7)----:- 1.0
  24. Math.floor( 0.0)----: 0.0
  25. Math.floor(- 0.0)----:- 0.0
  26. ------------------------------------------
  27. Math.random()----: 0.8979626325354049
  28. Math.random() 100----: 32.783762836248144
  29. ------------------------------------------
  30. Math.rint( 10.1)----: 10.0
  31. Math.rint( 10.7)----: 11.0
  32. Math.rint(- 10.5)----:- 10.0
  33. Math.rint(- 10.51)----:- 11.0
  34. Math.rint(- 10.2)----:- 10.0
  35. Math.rint( 9)----: 9.0
  36. ------------------------------------------
  37. Math.round( 10.1)----: 10
  38. Math.round( 10.7)----: 11
  39. Math.round(- 10.5)----:- 10
  40. Math.round(- 10.51)----:- 11
  41. Math.round(- 10.2)----:- 10
  42. Math.round( 9)----: 9
  43. ------------------------------------------
  44. Math.nextUp( 1.2)----: 1.2000000000000002
  45. Math.nextDown( 1.2)----: 1.1999999999999997
  46. Math.nextAfter( 1.2, 2.7)----: 1.2000000000000002
  47. Math.nextAfter( 1.2, - 1)----: 1.1999999999999997






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值