在java中浅谈Math类中的常用方法

通过最近的学习,学到了一些的Math类中的常见方法
package org.stm.demo;

public class Test {

public static void main(String[] args) {

    /**
     * Math.abs(绝对值)
     */
    System.out.println(Math.abs(-11.8));    //11.8
    System.out.println(Math.abs(-0.0));     //0.0
    /**
     * Math.ceil(天花板)
     */
    System.out.println(Math.ceil(-11.8));   //-11.0
    System.out.println(Math.ceil(10.8));    //11.0  
    System.out.println(Math.ceil(-0.6));    //-0.0  
    System.out.println(Math.ceil(0.0));     //0.0  
    System.out.println(Math.ceil(-0.0));    //-0.0  

    /**
     * Math.floor(地板)
     */
    System.out.println(Math.floor(-11.1));  //-12.0  
    System.out.println(Math.floor(11.7));   //11.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 

    /**
     * Math.max(arg0,arg1)
     * Math.min(arg0,arg1)
     */
    System.out.println(Math.max(-11.1, -11));   //-11.0  
    System.out.println(Math.max(11.7, 11));     //11.7  
    System.out.println(Math.max(0.0, -0.0));    //0.0  

    /** 
     * round 四舍五入,float时返回int值,double时返回long值 
     *          Math.round(x) = Math.floor(x+0.5)
     */  
    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  

    /** 
     * 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  
}

}

如果大家有疑问,可评论,也可以到APl中查到相应的用法以及到编辑器里面不妨一试!加油>>>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值