Math的几个方法Math.round()、Math.ceil()、Math.floor()和Math.abs()记录一下

Math.round()

就是数学中的四舍五入,举例:

System.out.println("Math.round(1.2)="+Math.round(1.2));

System.out.println("Math.round(1.5)="+Math.round(1.5));

System.out.println("Math.round(1.7)="+Math.round(1.7));

System.out.println("Math.round(-1.2)="+Math.round(-1.2));

System.out.println("Math.round(-1.5)="+Math.round(-1.5));

System.out.println("Math.round(-1.7)="+Math.round(-1.7));

结果输出为:

Math.round(1.2)=1

Math.round(1.5)=2

Math.round(1.7)=2

Math.round(-1.2)=-1

Math.round(-1.5)=-1

Math.round(-1.7)=-2

Math.ceil()

ceil就是天花板的意思,就是对上取整,举例:

        System.out.println("Math.ceil(1.2)="+Math.ceil(1.2));

        System.out.println("Math.ceil(1.5)="+Math.ceil(1.5));

        System.out.println("Math.ceil(1.7)="+Math.ceil(1.7));

        System.out.println("Math.ceil(-1.2)="+Math.ceil(-1.2));

        System.out.println("Math.ceil(-1.5)="+Math.ceil(-1.5));

        System.out.println("Math.ceil(-1.7)="+Math.ceil(-1.7));

结果输出为:

Math.ceil(1.2)=2.0

Math.ceil(1.5)=2.0

Math.ceil(1.7)=2.0

Math.ceil(-1.2)=-1.0

Math.ceil(-1.5)=-1.0

Math.ceil(-1.7)=-1.0

Math.floor()

floor就是地板的意思,就是对下取整,举例:

System.out.println("Math.floor(1.2)="+Math.floor(1.2));

System.out.println("Math.floor(1.5)="+Math.floor(1.5));

System.out.println("Math.floor(1.7)="+Math.floor(1.7));

System.out.println("Math.floor(-1.2)="+Math.floor(-1.2));

System.out.println("Math.floor(-1.5)="+Math.floor(-1.5));

System.out.println("Math.floor(-1.7)="+Math.floor(-1.7));

结果输出为:

Math.floor(1.2)=1.0

Math.floor(1.5)=1.0

Math.floor(1.7)=1.0

Math.floor(-1.2)=-2.0

Math.floor(-1.5)=-2.0

Math.floor(-1.7)=-2.0

Math.abs()

就是取绝对值,举例:

        System.out.println("Math.abs(1.2)="+Math.abs(1.2));

        System.out.println("Math.abs(1.5)="+Math.abs(1.5));

        System.out.println("Math.abs(1.7)="+Math.abs(1.7));

        System.out.println("Math.abs(-1.2)="+Math.abs(-1.2));

        System.out.println("Math.abs(-1.5)="+Math.abs(-1.5));

        System.out.println("Math.abs(-1.7)="+Math.abs(-1.7));

结果输出为:

Math.abs(1.2)=1.2

Math.abs(1.5)=1.5

Math.abs(1.7)=1.7

Math.abs(-1.2)=1.2

Math.abs(-1.5)=1.5

Math.abs(-1.7)=1.7

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容