• Math.round(-1.5) 等于多少?


    运行结果: -1

    JDK 中的 java.lang.Math 类

    • round() :返回四舍五入,负 .5 小数返回较大整数,如 -1.5 返回 -1。
    • ceil() :返回小数所在两整数间的较大值,如 -1.5 返回 -1。
    • tail() :返回小数所在两整数间的较小值,如 -1.5 返回 -2。

    测试代码:

    System.out.println("Math.round(1.4)=" + Math.round(1.4));
    System.out.println("Math.round(-1.4)=" + Math.round(-1.4));
    System.out.println("Math.round(1.5)=" + Math.round(1.5));
    System.out.println("Math.round(-1.5)=" + Math.round(-1.5));
    System.out.println("Math.round(1.6)=" + Math.round(1.6));
    System.out.println("Math.round(-1.6)=" + Math.round(-1.6));
    System.out.println();
    
    System.out.println("Math.ceil(1.4)=" + Math.ceil(1.4));
    System.out.println("Math.ceil(-1.4)=" + Math.ceil(-1.4));
    System.out.println("Math.ceil(1.5)=" + Math.ceil(1.5));
    System.out.println("Math.ceil(-1.5)=" + Math.ceil(-1.5));
    System.out.println("Math.ceil(1.6)=" + Math.ceil(1.6));
    System.out.println("Math.ceil(-1.6)=" + Math.ceil(-1.6));
    System.out.println();
    
    System.out.println("Math.floor(1.4)=" + Math.floor(1.4));
    System.out.println("Math.floor(-1.4)=" + Math.floor(-1.4));
    System.out.println("Math.floor(1.5)=" + Math.floor(1.5));
    System.out.println("Math.floor(-1.5)=" + Math.floor(-1.5));
    System.out.println("Math.floor(1.6)=" + Math.floor(1.6));
    System.out.println("Math.floor(-1.6)=" + Math.floor(-1.6));

    打印结果:

    Math.round(1.4)=1
    Math.round(-1.4)=-1
    Math.round(1.5)=2
    Math.round(-1.5)=-1
    Math.round(1.6)=2
    Math.round(-1.6)=-2
    
    Math.ceil(1.4)=2.0
    Math.ceil(-1.4)=-1.0
    Math.ceil(1.5)=2.0
    Math.ceil(-1.5)=-1.0
    Math.ceil(1.6)=2.0
    Math.ceil(-1.6)=-1.0
    
    Math.floor(1.4)=1.0
    Math.floor(-1.4)=-2.0
    Math.floor(1.5)=1.0
    Math.floor(-1.5)=-2.0
    Math.floor(1.6)=1.0
    Math.floor(-1.6)=-2.0

      

    来一道刷了进BAT的面试题?

  • 相关阅读:
    html实现滚动播报(原生JS实现)
    移动端H5页面惯性滑动监听
    简单线条、任意填充色的小图标(含自制方法)
    git和github新手安装使用教程(三步入门)
    node.js的安装配置——前端的配置
    Sublime Text 3 一些简单使用
    $.ajax() 方法的理解
    前端知识点
    (6)一些工作和生活的经验分享,以后还会不断补充添加
    (5.1)Opencv库学习第二部分
  • 原文地址:https://www.cnblogs.com/ConstXiong/p/11808173.html
Copyright © 2020-2023  润新知