Math函数floor、round、ceil的区别

floor 返回不大于的最大整数

round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果为12,Math.round(-11.5)的结果为-11。

ceil 则是不小于他的最小整数

Math.floor Math.round Math.ceil
1.4 1
1.5 1
1.6 1
-1.4 -2 -1  -1 
-1.5 -2 -1  -1 
-1.6 -2 -2  -1 

测试程序如下:

[java] view plaincopyprint?

  1. public class MyTest {
  2. public static void main(String[] args) {
  3. double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 };
  4. for (double num : nums) {
  5. test(num);
  6. }
  7. }
  8. private static void test(double num) {
  9. System.out.println("Math.floor(" + num + ")=" + Math.floor(num));
  10. System.out.println("Math.round(" + num + ")=" + Math.round(num));
  11. System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num));
  12. }
  13. }

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

时间: 2024-08-12 08:55:02

Math函数floor、round、ceil的区别的相关文章

问题:oracle floor;结果:Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明

Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明 (2011-04-06 16:10:35) 转载▼ 标签: 谈 分类: 渐行渐远 FLOOR——对给定的数字取整数位 SQL> select floor(2345.67) from dual; FLOOR(2345.67) -------------- 2345 CEIL-- 返回大于或等于给出数字的最小整数 SQL> select ceil(3.1415927) from dual; CEIL(3.14

Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明

Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明 FLOOR——对给定的数字取整数位SQL> select floor(2345.67) from dual; FLOOR(2345.67)--------------2345 CEIL-- 返回大于或等于给出数字的最小整数SQL> select ceil(3.1415927) from dual; CEIL(3.1415927)---------------              4 ROUND——按

mysql取整,小数点处理函数floor(), round()

mysql数值处理函数floor与round 在mysql中,当处理数值时,会用到数值处理函数,如有一个float型数值2.13,你想只要整数2,那就需要下面的函数floor与round. floor:函数只返回整数部分,小数部分舍弃. round:函数四舍五入,大于0.5的部分进位,不到则舍弃.与floor不同.如下测试 mysql> select floor(5.1); +------------+ | floor(5.1) | +------------+ |          5 | +

Math中floor,round和ceil的区别

floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数(当-1.5时可见,四舍五入后得到的结果不是我们期待的,解决办法是先对他取绝对值,然后在用round方法) round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果为12,Math.round(-11.5)的结果为-11. ceil 则是不小于他的最小整数 example: src value Math.

Java Math的 floor,round和ceil的总结

floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果为12,Math.round(-11.5)的结果为-11. ceil 则是不小于他的最小整数 看例子   Math.floor Math.round Math.ceil 1.4 1 1 2 1.5 1 2 2 1.6 1 2 2 -1.4 -2 -1

JAVA除法保留小数点后两位的两种方法 Java Math的 floor,round和ceil的总结

floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果为12,Math.round(-11.5)的结果为-11. ceil 则是不小于他的最小整数 看例子   Math.floor Math.round Math.ceil 1.4 1 1 2 1.5 1 2 2 1.6 1 2 2 -1.4 -2 -1

区分舍入函数fix/round/ceil/floor

1)fix(n)的意义是取小于n的整数(是向零点舍入的意思是往零的方向上靠),这是一类应用在整数取值上的函数,就如同以前我们所研究的求整问题: 例如:fix(pi)=3 ; fix(3.5)=3;  fix(-3.5)=-3; 这样举例的意思是说明这与四舍五入无关,就是纯粹的一种取值函数. 2)round(n)的意思是纯粹的四舍五入,意思与我们以前数学中的四舍五入是一样的!round(pi)=3;round(3.5)=4;round(-3.5)=-4;round(-3.1)=-3;这一点注意与f

JavaScript中Math--random()/floor()/round()/ceil()

Math.random():返回0-1之间的任意数,不包括0和1: Math.floor(num):返回小于等于num的整数,相当于四舍五入的四舍,不五入:例子:Math.floor(1.0);Math.floor(1.5);Math.floor(1.9);都返回1: Math.round(num):num进行四舍五入:例子:Math.round(1.0);Math.round(1.4);返回1;/Math.round(1.5);Math.round(1.9);返回2; Math.ceil(nu

Math类中round、ceil和floor方法的功能

Java中的Math工具类用来完成除+.-.*./.%等基本运算以外的复杂运算,位于java.lang包下,Math类的构造器全是私有的(private),因此无法创建Math类的对象,Math类的方法全是类方法,可以直接通过类名来调用它们.下面重点介绍Math类中经常用到的几个方法,也是面试时经常被问到的知识点. 1.round round方法表示四舍五入.round意为“环绕”,其原理是在原数字的基础上先加上0.5再向下取整,它的返回值为int类型,例如,Math.round(11.5)等于