Math.floor,Math.ceil,Math.rint,Math.round用法
一、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用法的更多相关文章
- 精灵方向移动问题[math.floor]
local xd = math.cos(math.rad(self._direction));--self._direction方向角度 local yd = math.sin(math.rad(se ...
- JavaScript Math.floor() 方法
定义和用法: floor() 方法可对一个数进行下舍入. 语法: Math.floor(x); x:必须参数,可以是任意数值或表达式: 返回值: 小于等于 x,且与 x 最接近的整数. 说明: flo ...
- math.floor()函数的用法
floor() 返回数字的下舍整数. 语法 以下是 floor() 方法的语法: import math math.floor( x ) 注意:floor()是不能直接访问的,需要导入 math 模块 ...
- JS对象 向下取整floor() floor() 方法可对一个数进行向下取整。 语法: Math.floor(x)
向下取整floor() floor() 方法可对一个数进行向下取整. 语法: Math.floor(x) 参数说明: 注意:返回的是小于或等于x,并且与 x 最接近的整数. 我们将在不同的数字上使用 ...
- Python - 基本数据处理函数round()、int()、floor()、ceil()
前言 对每位程序员来说,在编程过程中数据处理是不可避免的,很多时候都需要根据需求把获取到的数据进行处理,取整则是最基本的数据处理.取整的方式则包括向下取整.四舍五入.向上取整等等.下面就来看看在Pyt ...
- 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( ...
- JavaScript的几种Math函数,random(),ceil(),round(),floor()
1.Math.random():返回 0 ~ 1 之间的随机数.2.Math.ceil():返回值:返回大于或等于x,并且与之最接近的整数(如果x是正数,则把小数"入":如果x是负 ...
- 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( ...
- Java Math floor round ceil 函数
public final class Math extends Object public static double floor(double a) public static long round ...
随机推荐
- 团队项目(第三周)—GG队
需求改进&系统设计 队员 学号 叶尚文(队长) 3116008802 蔡晓晴 3216008808 杜婷萱 3216008809 龙剑初 3116004647 于泽浩 3116004661 一 ...
- CAS实战の简介
一.SSO简介 单点登录的英文名称为Single Sign-On,简写为SSO,它是一个用户认证的过程,允许用户一次性进行认证之后,就访问系统中不同的应用:而不需要访问每个应用时,都重新输入密码.IB ...
- Linux服务器目录空间不足解决措施
一般情况下工作环境中我们的服务或数据库文件都会存储在一个单独挂载的分区中,一般占空间比较大的大多就是服务的运行日志以及数据库文件,当我们分区的可用空间不足时就需要我们对分区进行扩容,或者找其它方法 ...
- Spring Boot 2 实践记录之 Powermock 和 SpringBootTest
由于要代码中使用了 Date 类生成实时时间,单元测试中需要 Mock Date 的构造方法,以预设其行为,这就要使用到 PowerMock 在 Spring Boot 的测试套件中,需要添加 @Ru ...
- Asp.net MVC + Redis(Linux安装Redis)
最近有幸在工作中用到了redis,玩的还算开心.但是发现Redis在Windows上并不是满血状态的,所以决定安装一个Linux的虚拟机,让Redis在Linux上运行. 虚拟环境 虚拟机,我已经玩了 ...
- JS获取用户的Ip地址
在网站中通常需要获取使用者的ip地址,获取抵制的方式有很多,这里就简单介绍一下js获取用户ip地址 /*使用的新浪的ip查询api,根据返回的数据进行判断*/ <script src=" ...
- Dependency injection configurations into views in asp.net core
本文展示如何在ASP.NET Core MVC Application Razor视图中注入和使用应用程序的配置信息. 将配置信息添加到appsettings.json中: { "Loggi ...
- .net core cache使用
整理下.net core cache的使用 Nuget安装 Microsoft.Extensions.Caching.Memory using Microsoft.Extensions.Cachi ...
- 嵌入的资源 和 Resource
我们将资源文件添加至.net C#工程时,文件的生成操作有多种可选方式.通常用的多的是两种:[嵌入的资源]和[Resource],如果从需要从代码中使用这些资源文件,不同生成操作则对应不同的引用方式: ...
- JQuery Mobile - 为什么绑定事件后会被多次执行?
JQuery Mobile 在绑定事件时候,发现会被多次执行,为什么啊? 原来,jquery click 不是替换原有的function ,而是接着添加,所以才会执行次数越来越多,怎么办才能按需实现 ...