一、Math.floor函数讲解

  floor原意:地板。Math.floor函数是求一个浮点数的地板,就是求一个最接近它的整数,它的值小于或等于这个浮点数。看下面的例子:

package com.qiyuan.util;

public class GetInt {

    /**
* Math.floor函数测试
* @param args
*/
public static void main(String[] args)
{
System.out.println("Math.floor(-1.1): " + Math.floor(-1.1));
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(0.1): " + Math.floor(0.1));
System.out.println("Math.floor(0.5): " + Math.floor(0.5));
System.out.println("Math.floor(0.6): " + Math.floor(0.6));
System.out.println("Math.floor(1.1): " + Math.floor(1.1));
System.out.println("Math.floor(1.5): " + Math.floor(1.5));
System.out.println("Math.floor(1.6): " + Math.floor(1.6));
} }

  结果为:

Math.floor(-1.1): -2.0
Math.floor(-1.5): -2.0
Math.floor(-1.6): -2.0
Math.floor(0.1): 0.0
Math.floor(0.5): 0.0
Math.floor(0.6): 0.0
Math.floor(1.1): 1.0
Math.floor(1.5): 1.0
Math.floor(1.6): 1.0

二、Math.ceil函数讲解

  ceil愿意:天花板。Math.ceil函数执行的是向上取整计算,它返回的是大于或等于函数参数,并且与之最接近的整数。看下面的例子:

package com.qiyuan.util;

public class GetInt {

    /**
* Math.ceil函数测试
* @param args
*/
public static void main(String[] args)
{
System.out.println("Math.ceil(-1.1): " + Math.ceil(-1.1));
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(0.1): " + Math.ceil(0.1));
System.out.println("Math.ceil(0.5): " + Math.ceil(0.5));
System.out.println("Math.ceil(0.6): " + Math.ceil(0.6));
System.out.println("Math.ceil(1.1): " + Math.ceil(1.1));
System.out.println("Math.ceil(1.5): " + Math.ceil(1.5));
System.out.println("Math.ceil(1.6): " + Math.ceil(1.6));
} }

  结果为:

Math.ceil(-1.1): -1.0
Math.ceil(-1.5): -1.0
Math.ceil(-1.6): -1.0
Math.ceil(0.1): 1.0
Math.ceil(0.5): 1.0
Math.ceil(0.6): 1.0
Math.ceil(1.1): 2.0
Math.ceil(1.5): 2.0
Math.ceil(1.6): 2.0

三、Math.rint函数讲解

  Math.rint函数返回最接近参数的整数,如果有2个数同样接近,则返回偶数的那个。

/**
* Math.rint函数测试
* @param args
*/
public static void main(String[] args)
{
System.out.println("Math.rint(-1.1): " + Math.rint(-1.1));
System.out.println("Math.rint(-1.5): " + Math.rint(-1.5));
System.out.println("Math.rint(-1.6): " + Math.rint(-1.6));
System.out.println("Math.rint(0.1): " + Math.rint(0.1));
System.out.println("Math.rint(0.5): " + Math.rint(0.5));
System.out.println("Math.rint(0.6): " + Math.rint(0.6));
System.out.println("Math.rint(1.1): " + Math.rint(1.1));
System.out.println("Math.rint(1.5): " + Math.rint(1.5));
System.out.println("Math.rint(1.6): " + Math.rint(1.6));
}

  结果为:

Math.rint(-1.1): -1.0
Math.rint(-1.5): -2.0
Math.rint(-1.6): -2.0
Math.rint(0.1): 0.0
Math.rint(0.5): 0.0
Math.rint(0.6): 1.0
Math.rint(1.1): 1.0
Math.rint(1.5): 2.0
Math.rint(1.6): 2.0

四、Math.round函数讲解

  Math.round方法,它表示"四舍五入",算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整

    /**
* Math.round函数测试
* @param args
*/
public static void main(String[] args)
{
System.out.println("Math.round(-1.1): " + Math.round(-1.1));
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(0.1): " + Math.round(0.1));
System.out.println("Math.round(0.5): " + Math.round(0.5));
System.out.println("Math.round(0.6): " + Math.round(0.6));
System.out.println("Math.round(1.1): " + Math.round(1.1));
System.out.println("Math.round(1.5): " + Math.round(1.5));
System.out.println("Math.round(1.6): " + Math.round(1.6));
}

  结果为:

Math.round(-1.1): -1
Math.round(-1.5): -1
Math.round(-1.6): -2
Math.round(0.1): 0
Math.round(0.5): 1
Math.round(0.6): 1
Math.round(1.1): 1
Math.round(1.5): 2
Math.round(1.6): 2

Math.floor,Math.ceil,Math.rint,Math.round用法的更多相关文章

  1. 精灵方向移动问题[math.floor]

    local xd = math.cos(math.rad(self._direction));--self._direction方向角度 local yd = math.sin(math.rad(se ...

  2. JavaScript Math.floor() 方法

    定义和用法: floor() 方法可对一个数进行下舍入. 语法: Math.floor(x); x:必须参数,可以是任意数值或表达式: 返回值: 小于等于 x,且与 x 最接近的整数. 说明: flo ...

  3. math.floor()函数的用法

    floor() 返回数字的下舍整数. 语法 以下是 floor() 方法的语法: import math math.floor( x ) 注意:floor()是不能直接访问的,需要导入 math 模块 ...

  4. JS对象 向下取整floor() floor() 方法可对一个数进行向下取整。 语法: Math.floor(x)

    向下取整floor() floor() 方法可对一个数进行向下取整. 语法: Math.floor(x) 参数说明: 注意:返回的是小于或等于x,并且与 x 最接近的整数. 我们将在不同的数字上使用 ...

  5. Python - 基本数据处理函数round()、int()、floor()、ceil()

    前言 对每位程序员来说,在编程过程中数据处理是不可避免的,很多时候都需要根据需求把获取到的数据进行处理,取整则是最基本的数据处理.取整的方式则包括向下取整.四舍五入.向上取整等等.下面就来看看在Pyt ...

  6. java中常用到的math方法(Math.PI、Math.random()、Math.abs(double)、Math.floor(double)、Math.ceil(double)、Math.round(double))

    public class MathDemo { public static void main(String args[]){ /** * abs求绝对值 */ System.out.println( ...

  7. JavaScript的几种Math函数,random(),ceil(),round(),floor()

    1.Math.random():返回 0 ~ 1 之间的随机数.2.Math.ceil():返回值:返回大于或等于x,并且与之最接近的整数(如果x是正数,则把小数"入":如果x是负 ...

  8. Javascript Math.ceil()与Math.round()与Math.floor()区别

    Math.ceil()向上舍入 1 2 3 alert(Math.ceil(20.1)) //输出 21 alert(Math.ceil(20.5)) //输出 21 alert(Math.ceil( ...

  9. Java Math floor round ceil 函数

    public final class Math extends Object public static double floor(double a) public static long round ...

随机推荐

  1. 网友写的解决uniGUI限制的方法

    群友写的解决uniGUI试用版限制修改SessionTimeOut,思路很精巧,贴过来分享,感谢朋友的奉献.当然,如果真正用uniGUI实做项目,买份正版是正道! var   UniServerOpt ...

  2. 8086汇编语言(1)虚拟机安装ms-dos 7.1

    8086汇编语言(1)虚拟机安装ms-dos 7.1 文/玄魂 前言 在开始这一系列文章之前,我想先说下,对于古董级的8086汇编到底还以有没有学习的必要.这里我说下我要从8086开始学习,而不是从w ...

  3. c# is 和 as 的区别和使用

    1:is 是判断类型,用于检查对象是否与给定类型兼容,不成功则不会抛出异常,如果兼容则返回true,如果不兼容则返回false.在进行类型转换之前用 f (P_obj is System.String ...

  4. application/force-download 不生效

    不管用什么方式都无法下载txt 设置application/force-download也不生效 很无奈 胡搞瞎搞 最终解决方案:但是没搞明白什么原理 问题解决 @RequestMapping(val ...

  5. Kettle 使用入门

    Kettle是一款国外开源的ETL工具,纯java编写,可以在Window.Linux.Unix上运行,数据抽取高效稳定. 本文介绍一个入门例子:使用Kettle从动态指定的文件名对应的文本文件里抽取 ...

  6. Centos 7 GCC 7.3编译器安装方法及C++17标准测试示例

    1.下载gcc-7.3.0源码 http://mirror.linux-ia64.org/gnu/gcc/releases/gcc-7.3.0/ 2.下载编译依赖 [root@localhost ~] ...

  7. 程序媛计划——mysql连接表

    #inner join等值连接/内连接 mysql> select * from info; +------+-------------+----------+ | name | phone | ...

  8. A - Subsequence (算法 二分 )

    点击打开链接 A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 1 ...

  9. Django安装(第一个项目)

    day60 从系统中选择已存在的解释器. 新建Django项目         命令行创建:             django-admin startproject mysite          ...

  10. hdoj1373 Channel Allocation(极大团)

    题意是有若干个接收器,给出每个接收器的相邻接收器.相邻的接收器不能使用同一信号频道.问所需要的信号频道数. 求该无向图的极大团. #include<iostream> #include&l ...