zoukankan      html  css  js  c++  java
  • 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的面试题?

  • 相关阅读:
    python 约束与异常处理
    ActiveMQ
    SpringMVC项目,启动项目怎么总是报找不到log4j.properties文件
    java 字符串处理
    java面向对象学习笔记
    JSONArray遍历
    get/post方式调用http接口
    IO流认识
    Apache Mina 入门实例
    “wsimport -keep ”生成客户端报错“Use of SOAP Encoding is not supported.”
  • 原文地址:https://www.cnblogs.com/ConstXiong/p/11808173.html
Copyright © 2011-2022 走看看