一、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. [mobile]移动端页面没有重新请求时,刷新页面代码

    <input type="hidden" value="yes" id="id_if_reload" /> <script ...

  2. 如何实现让你的网站支持Google Roboto Font

    最近在写一个移动端的web项目,designer设计的页面效果图就是使用Google Roboto Font.The Roboto Font 是Google为Android 4.0 Ice-Cream ...

  3. SSD测试第一神器——FIO

    原文 地址 http://www.ssdfans.com/ssd%E6%B5%8B%E8%AF%95%E7%AC%AC%E4%B8%80%E7%A5%9E%E5%99%A8-fio-2/ 对于SSD性 ...

  4. The transaction associated with this command is not the connection's active transaction

    The fix is fairly simple: if you want a Dapper query to participate in a connection, explicitly deno ...

  5. SQL Server OS 调度

    --SQL SERVER OS 采用合作模式的线程调度模式,即除非Worker主动放弃CPU,否则SQL OS 不会强制剥夺其CPU,从而减少Context Switch --默认设置下,SQL SE ...

  6. httprunner 使用总结

    HttpRunner 概念 HttpRunner 是一款面向 HTTP(S) 协议的通用测试框架,只需编写维护一份 YAML/JSON 脚本,即可实现自动化测试.性能测试.线上监控.持续集成等多种测试 ...

  7. Linux 操作日志

    这里: /var/log/messages

  8. vue 学前班001(基础概念)

    1  学习目标 通过这一节,你会学会: 1.目前前端技术使用的趋势 2.什么是MVVM 3.Vue.js的两大核心 4.Vue.js的适用场景  诞生背景 近几年来,得益于手机设备的普及和性能的提升, ...

  9. MySQL远程登陆解决

    第一句:以权限用户root登录 第二句:选择mysql库 第三句:查看mysql库中的user表的host值(即可进行连接访问的主机/IP名称) 第四句:修改host值(以通配符%的内容增加主机/IP ...

  10. 对Spring 及SpringMVC的理解

    spring是一个轻型容器(light-weight Container),其核心是Bean工厂(Bean Factory),用以构造我们所需要的M(Model).在此基础之上,Spring提供了AO ...