java基础-Math类常用方法介绍

              java基础-Math类常用方法介绍

                              作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.Math类概念

  Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。 

  类似这样的工具类(代表能够完成一些列功能的类),其所有方法均为静态方法,并且构造方法一般都被私有化啦,也就是不能创建对象。

二.Math的常用方法

1>.获取参数的绝对值(public static int abs(int i) )

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.demo;
 8 
 9 public class MathDemo {
10     public static void main(String[] args) {
11         int number = -100;
12         int src = Math.abs(number);
13         System.out.println(src);
14     }
15 }

2>.返回大于或者等于参数d的最小整数(public double ceil(double d))

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.demo;
 8 
 9 public class MathDemo {
10     public static void main(String[] args) {
11         double d1 = -9.8;
12         double res = Math.ceil(d1);
13         System.out.println(res);
14         double d2 = 9.8;
15         double res2 = Math.ceil(d2);
16         System.out.println(res2);
17     }
18 }
19 
20 
21 /*
22 以上代码执行结果如下:
23 -9.0
24 10.0
25 */

3>.返回小于或者等于参数d的最大整数(public static double floor(double d))

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.demo;
 8 
 9 public class MathDemo {
10     public static void main(String[] args) {
11         double d1 = -9.8;
12         double res1 = Math.floor(d1);
13         System.out.println(res1);
14         
15         double d2 = 9.8;
16         double res2 = Math.floor(d2);
17         System.out.println(res2);
18     }
19 }
20 
21 
22 /*
23 以上代码执行结果如下:
24 -10.0
25 9.0
26 */

4>.a的b次方(public static double pow(double a,double b))

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.demo;
 8 
 9 public class MathDemo {
10     public static void main(String[] args) {
11         double d = Math.pow(5, 2);
12         System.out.println(d);
13         
14     }
15 }
16 
17 
18 /*
19 以上代码执行结果如下:
20 25.0
21 */

5>.返回参数的平方根(public static double sqrt(double d))

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.demo;
 8 
 9 public class MathDemo {
10     public static void main(String[] args) {
11         double d = Math.sqrt(81);
12         System.out.println(d);
13     }
14 }
15 
16 
17 /*
18 以上代码执行结果如下:
19 9.0
20 */

6>.返回随机数0.0~1.0之间(public static double random()),该方法的来源是Random类。

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.demo;
 8 
 9 public class MathDemo {
10     public static void main(String[] args) {
11         for(int i=0;i<10;i++) {
12             double d = Math.random();
13             System.out.println(d);
14         }
15     }
16 }
17 
18 
19 /*
20 以上代码执行结果如下:(以下的数字都是随机生成的)
21 0.5086409315812314
22 0.6433798120519407
23 0.4186266302459938
24 0.9847416233251283
25 0.6255795991641623
26 0.955791368581619
27 0.5175481624339179
28 0.7882189426101148
29 0.7437009908163126
30 0.49198607804636907
31 */

7>.获取参数的四舍五入值(public double round(double d))

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.demo;
 8 
 9 public class MathDemo {
10     public static void main(String[] args) {
11         double d = Math.round(5.4925);
12         System.out.println(d);
13         d = Math.round(6.425);
14         System.out.println(d);
15     }
16 }
17 
18 
19 /*
20 以上代码执行结果如下:(以下的数字都是随机生成的)
21 5.0
22 6.0
23 */

猜你喜欢

转载自www.cnblogs.com/yinzhengjie/p/8904204.html