Math的基本用法

来源:互联网 发布:如何办理电信cn2网络 编辑:程序博客网 时间:2024/06/17 01:25

Math的基本用法

public staticvoid main(String[] args) {

/**

* abs

* 求绝对值

*/

System.out.println(Math.abs(-5.1));// 5.1

/**

* addExact 

* 两个int类型的数相加

*/

System.out.println(Math.addExact(-2, 8));//6

/**

* ceil 

* 返回该数值的最大值

*/

System.out.println(Math.ceil(-5.1));//-5.0

/**

* floor 

* 返回该数值的最小值

*/

System.out.println(Math.floor(-5.1));//-6.0

/**

* floorDiv 

* 返回等于或小于代数商的最大整数值

*/

System.out.println(Math.floorDiv(-2, 8));//-1

/**

* max 

* 返回两个数中,最大的一个,min的话正好相反

*/

System.out.println(Math.max(-2, 3));//3

/**

* random 

* 获取一个大于或等于0.0,小于1.0的随机数

*/

System.out.println(Math.random());//0.3493585207879425  每次值都是不一样的

/**

* rint 

* 四舍五入取值

*/

System.out.println(Math.rint(-5.1));//-5.0

/**

* round 

* 返回int类型的值

*/

System.out.println(Math.round(-5.21));//-5

System.out.println(Math.round(-5));//-5

System.out.println(Math.round(5.2222));//5

System.out.println(Math.round(-5.0));//-5

}

1 0
原创粉丝点击