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 ...
随机推荐
- mac下能同时安装两个版本的xcode吗
http://www.cocoachina.com/bbs/read.php?tid-288160-page-1.html
- [leetcode] 15. Plus One
这道题其实让我意识到了我的英文水平还有待加强.... 题目如下: Given a non-negative number represented as an array of digits, plus ...
- matlab 生成mat文件
生成MAT文件 假如你的矩阵A.B都已赋值,现在需要将A,B矩阵保存成mat格式文件,执行如下命令:save mydata A B 就会把A B矩阵数据保存在yourdata.dat文件中了 保存指定 ...
- Jersey Client Post Bean参数
代码: public static void main(String[] args) { Student st = new Student("Adriana", "Bar ...
- SignalR 设计理念(二)
SignalR 设计理念(二) 实现客户端和服务器端的实时通讯. 前言: 客户端方法忽略大小写,主要原因基于是URL对大小写不敏感的问题,开发者之间为了更好的协同开发,定下的开发者协议. 问题阐述 客 ...
- js判断是否是数组
//判断是否是数组function isArray(obj) { return Object.prototype.toString.call(obj) === '[object Array]' ...
- C# 图片反色处理 图片夜间模式
项目属性-->生成-->允许不安全代码勾上. 代码: /// <summary> /// 反色处理 /// </summary> private Bitmap In ...
- brew - 更换国内源
brew如果不换成国内源,安装软件时候可能会出问题,不是安装不了就是速度很慢,所以使用它,更换国内游是比较好的选择! 我更换的是清华大学开源软件镜像站,打开shell窗口,依次执行下面命令: cd & ...
- FunDA(8)- Static Source:保证资源使用安全 - Resource Safety
我们在前面用了许多章节来讨论如何把数据从后台数据库中搬到内存,然后进行逐行操作运算.我们选定的解决方案是把后台数据转换成内存中的数据流.无论在打开数据库表或从数据库读取数据等环节都涉及到对数据库表这项 ...
- Python小白学习之路(二)—【Pycharm安装与配置】【创建项目】【运算符】【数据类型】
写在前面: 第二天的学习,感觉比昨天学习相对轻松一些,但是对于我这个编程语言功底很弱的人来说,还是稍稍微有些.....哈尔滨的天气一天天冷了下来,还飘着小雨,不过还是挺有意境的.充实而又忙碌的生活,让 ...