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

时间: 2024-08-01 03:31:25

Java语言中小数的取整的相关文章

Java语言中的定义变量、构造函数

day02 Java语言中的定义类.变量.方法.构造函数 一.概述: 在Java语言中,变量的定义和使用时非常常见和重要的:同时对后续的操作变量奠定基础,在这里定义和使用变量就要使用到我们前一节说到的数据类型了,它们两个就是绑定在一起使用的.我们回顾一下前一节学的数据类型. 学完变量以后就要学会使用变量去定义一些东西,去构造我们需要的一些方法来满足学习的需要.从而引申出了构造这一个词汇.构造就是创造的含义,通过创造一些东西来满足.下面就一一的来看一下. 二.定义"类"(class):

Java语言中学习数组、运算符、流程控制的一些理解

一.数组 1.数组的概念及作用 数组是相同数据类型的元素的集合:   数组本身是引用数据类型,即对象.但是数组可以存储基本数据类型,也可以存储引用数据类型. 例如: int [] a = new int [] {1,2,3,4,5,6,}; String [] s = new String [] {"小兔","小小兔","小小小兔",} : Employee [] e = Employee [10];(Employee是自定义类). 2.数组的声

Java语言中的----运算符

day05 Java语言中的----运算符 一.运算符概述: 运算符的使用在每一门开发语言中都会使用到,在不同的语言中也会有不同的使用规则.通过运算符我们可以联想到MySQL数据库中的运算符,这些都是差不多的,可能有些在使用上是不一样的.下面就来看看Java中的运算符. 二.运算符: 1.算术运算符: 主要包含:加.减.乘.除.取余(%).自加(++).自减(--) 2.赋值运算符: 就是等号(=) 3.位运算符: 主要包含:&.|.~.^.<<.>>.>>&g

Java千问:Java语言中最大的整数再加1等于多少?

已知Java语言中int类型所能表示的最大整数为2147483647,请问以下代码执行结果是什么?一部分人都会认为这段程序压根就无法通过编译,也有人认为,这段程序能够通过编译,但在运行时会抛出异常,但更多的人面对这道题目根本就无从下手.那么正确答案是什么呢?首先告诉大家,这段程序能够顺利通过编译,并且在运行时也不会出现异常,运行的结果是在控制台上输出了数字-2147483648!而-2147483648正好是Java语言中int类型所能表示的最小整数.这个运行结果可能会让很多人感到大跌眼镜,运行

Java语言中反射动态代理接口的解释与演示

Java语言中反射动态代理接口的解释与演示 Java在JDK1.3的时候引入了动态代理机制.可以运用在框架编程与平台编程时候捕获事件.审核数据.日志等功能实现,首先看一下设计模式的UML图解: 当你调用一个接口API时候,实际实现类继承该接口,调用时候经过proxy实现. 在Java中动态代理实现的两个关键接口类与class类分别如下: java.lang.reflect.Proxy java.lang.reflect.InvocationHandler 我们下面就通过InvocationHan

原码、反码、补码相关内容以及Java语言中是以哪一种码表示的

计算机中的数字是以二进制方式存储的,第一个二进制位为符号位,0代表正数,1代表负数 原码.反码.补码是计算机中存储数字使用的编码 1.原码.反码.补码的概念 原码:符号位加上这个数绝对值 例如正整数1的8位二进制原码为 00000001      负整数-1的8为二进制原码为 10000001 反码:正数的反码就是其本身,负数的反码就是在原码的基础上除符号位外所有的位取反 例如正整数1的8位二进制原码为 00000001 则其反码还为 00000001  负整数-1的8为二进制原码为 10000

C#以及Oracle中的上取整、下取整方法

1.C#中: 上取整——Math.Ceiling(Double),即返回大于或等于指定双精度浮点数的最大整数(也可称为取天板值): eg:  Math.Ceiling(1.01)=2;      Math.Ceiling(1.37)=2; 下取整——Math.Floor(Double),即返回小于或等于指定双精度浮点数的最大整数(也可称为取地板值): eg:  Math.Floor(1.99) =1;       Math.Floor(1.87) =1; 2.Oracle中: 上取整——ceil

在Java语言中调用存储过程、存储函数、包头、包体

需要拷贝连接Oracle的jar包,路径如下图所示: 连接Oracle数据库的代码: package demo.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCUtils { private static Stri

Java语言中使用OpenMP

从去年年中,开始学习Java,主要是维护公司用Java编写的服务器软件.目前,该服务器软件遇到一个问题,在下载大文件时,如果同时下载的用户很多, 服务器软件工作会出现异常,有的用户无法下载.服务器硬件基本上都是多核处理器,所以,如果能在Java语言中使用并行编程技术,使用OpenMP,可能 会提高服务器软件的性能. 今天,测试了一下,Java语言中也可以使用OpenMP.以下是详细测试过程: 1. 下载jomp1.0b.jar https://www2.epcc.ed.ac.uk/computi