zoukankan      html  css  js  c++  java
  • Java语言中小数的取整

             1、double java.lang.Math.floor(double a)

        返回最大的double值(在正无穷方向上最接近的)小于或等于参数a的一个数学意义上的整数,特例:

        如果参数值等于数学上的整数,则返回结果与参数值相同

        如果参数是NaN(非数值)或是无穷或是+0或是-0,则返回值和参数值相同

        Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer. Special cases:

        If the argument value is already equal to a mathematical integer, then the result is the same as the argument.

        If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.

    Math.floor(2.0)=2.0
    Math.floor(2.1)=2.0
    Math.floor(2.5)=2.0
    Math.floor(2.9)=2.0

    Math.floor(-2.0)=-2.0
    Math.floor(-2.1)=-3.0
    Math.floor(-2.5)=-3.0
    Math.floor(-2.9)=-3.0
     
    2、double java.lang.Math.ceil(double a)
    返回最小的double值(在负无穷方向最接近的)大于或等于参数a的一个数学意义上的整数,特例:
    如果参数值已经等于一个数学意义上的整数,则返回结果与参数相同
    如果参数是NaN(非数值)或者是无穷,或是+0,-0,则返回结果与参数相同
    如果参数值小于0或大于-1.0
     

    Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. Special cases:

    • If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
    • If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.
    • If the argument value is less than zero but greater than -1.0, then the result is negative zero.
    Math.ceil(2.0)=2.0
    Math.ceil(2.1)=3.0
    Math.ceil(2.5)=3.0
    Math.ceil(2.9)=3.0

    Math.ceil(-2.0)=-2.0
    Math.ceil(-2.1)=-2.0
    Math.ceil(-2.5)=-2.0
    Math.ceil(-2.9)=-2.0

    double java.lang.Math.rint(double a)

    Returns the double value that is closest in value to the argument and is equal to a mathematical integer. If two double values that are mathematical integers are equally close, the result is the integer value that is even. Special cases:

    • If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
    • If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.
    Math.rint(2.0)=2.0
    Math.rint(2.5)=2.0
    Math.rint(2.6)=3.0
    Math.rint(2.9)=3.0

    Math.rint(-2.0)=-2.0
    Math.rint(-2.5)=-2.0
    Math.rint(-2.6)=-3.0
    Math.rint(-2.9)=-3.0

    long java.lang.Math.round(double a)

    Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:

    (long)Math.floor(a + 0.5d)
    
    

    Special cases:

    • If the argument is NaN, the result is 0.
    • If the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE, the result is equal to the value of Long.MIN_VALUE.
    • If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to the value of Long.MAX_VALUE.
    Math.round(2.0)=2
    Math.round(2.5)=3
    Math.round(2.6)=3
    Math.round(2.9)=3

    Math.round(-2.0)=-2
    Math.round(-2.5)=-2
    Math.round(-2.6)=-3
    Math.round(-2.9)=-3


  • 相关阅读:
    【数据结构】并查集
    项目管理【12】 | 项目范围管理-收集需求
    项目管理【11】 | 项目范围管理-规划范围管理
    项目管理【10】 | 项目范围管理-范围管理概述
    Visual Studio代码远程调试方法
    项目管理【09】 | 项目整体管理-结束项目或阶段
    操作系统【2】Linux系统安装
    操作系统【1】Linux基础知识
    移动端开发案例【2】头部组件开发
    移动端开发案例【1】全局样式配置
  • 原文地址:https://www.cnblogs.com/tobebrave/p/4178430.html
Copyright © 2011-2022 走看看