Math类的三个方法比较: floor() ceil() round()
public class Test {
public static void main(String[] args) {
double d1 = 3.4, d2 = 3.6; //正数
double d3 = -3.4, d4 = -3.6; //负数
float f1 = 4.4F, f2 = 4.6F; //正数
float f3 = -4.4F, f4 = -4.6F; //负数
//floor()方法只能接收double类型,返回double类型
//向下取整,返回小于参数的最大整数
System.out.println(Math.floor(d1));//3.0
System.out.println(Math.floor(d2));//3.0
System.out.println(Math.floor(d3));//-4.0
System.out.println(Math.floor(d4));//-4.0
//ceil()方法只能接收double类型,返回double类型
//向上取整,返回大于参数的最小整数
System.out.println(Math.ceil(d1));//4.0
System.out.println(Math.ceil(d2));//4.0
System.out.println(Math.ceil(d3));//-3.0
System.out.println(Math.ceil(d4));//-3.0
//round()方法可以接收double类型,返回long类型
//表示“四舍五入”,算法为Math.floor(x+0.5),即将参数加上0.5后再向下取整
System.out.println(Math.round(d1));//
System.out.println(Math.round(d2));//
System.out.println(Math.round(d3));//-3
System.out.println(Math.round(d4));//-4
//round()方法可以接收float类型,返回int类型
System.out.println(Math.round(f1));//
System.out.println(Math.round(f2));//
System.out.println(Math.round(f3));//-4
System.out.println(Math.round(f4));//-5
}
}
Math类的三个方法比较: floor() ceil() round()的更多相关文章
- Java关于Math类的三个取整方法
0x01 在java的Math类中有三个关于浮点数取整数的方法,分别是ceil (向上取整) floor(向下取整) round(四舍五入) 三个方法 0x02 ceil 向上取整,取整后总是比原来的 ...
- [转]Javascript定义类的三种方法
作者: 阮一峰 原文地址:http://www.ruanyifeng.com/blog/2012/07/three_ways_to_define_a_javascript_class.html 将近2 ...
- matlab中fix, floor, ceil, round 函数的使用方法
转载: https://www.ilovematlab.cn/thread-91895-1-1.html Matlab取整函数有: fix, floor, ceil, round.具体应用方法如下: ...
- MATLAB中取整函数(fix, floor, ceil, round)的使用
MATLAB取整函数 1)fix(x) : 截尾取整. >> fix( [3.12 -3.12]) ans = 3 -3(2)floor(x):不超过x 的最大整数.(高斯取整) & ...
- paper 68 :MATLAB中取整函数(fix, floor, ceil, round)的使用
MATLAB取整函数 1)fix(x) : 截尾取整. >> fix( [3.12 -3.12]) ans = 3 -3 (2)floor(x):不超过x 的最大整数.(高 ...
- java数学函数Math类中常用的方法
Math类提供了常用的一些数学函数,如:三角函数.对数.指数等.一个数学公式如果想用代码表示,则可以将其拆分然后套用Math类下的方法即可. Math.abs(12.3); ...
- C#的math类的全部运算方法
Abs 返回指定数字的绝对值.Acos 返回余弦值为指定数字的角度.Asin 返回正弦值为指定数字的角度.Atan 返回正切值为指定数字的角度.Atan2 返回正切值为两个指定数字的商的角度.BigM ...
- mathlab之floor,ceil,round,int以及fix函数
建议自己动手敲敲,网上很多人自己都没搞清楚然后好多错的.毕竟自己亲眼看到结果才有说服力. 以下是我亲眼见到的结果. 1.double floor(double)函数 floor()函数是常用的取整函数 ...
- 关于Math类的round、floor、ceil三个方法
一.Math类这三个方法的简介 1.round():取最接近的值. 对于这个方法,查看源代码,其实现如下: public static long round(double a) { if (a != ...
随机推荐
- sqlplus rlwrap readline
- 让IE依据HTML头标签选择显示模式
文件兼容性用于定义让IE如何编译你的网页.此文件解释文件兼容性,如何指定你网站的文件兼容性模式以及如何判断一个网页该使用的文件模式. 前言 为了帮助确保你的网页在所有未来的IE版本都有一致的外观,IE ...
- ES6系列_10之Symbol在对象中的作用
在ES5中 对象属性名都是字符串,这容易造成属性名的冲突,比如,你使用了一个他人提供的对象,但又想为这个对象添加新的方法(mixin 模式),新方法的名字就有可能与现有方法产生冲突,于是 ES6 引入 ...
- TCP/IP协议 socket
TCP/IP四层协议 TCP/IP概念 tcp/ip协议是主机接入互联网以及接入互联网的两台主机通信的标准. 数据帧概念 数据帧 |-- 包头 | |--源地址 | |--目标地址 | |--数据类型 ...
- Space-vim的.spacevim配置备份
安装 windows安装 配置 在C盘的用户目录下,有一个'.spacevim'的文件,可以修改你要的配置 " Let Vim and NeoVim shares the same plug ...
- zmq消息订阅
一个需求,用户预约了手机超时没有使用,要通知到预约的用户“设备超时”. 我本来是自己这一端计时然后超时后推送通知的. 但是上海测说他那边计时,然后释放手机.我这边只要订阅他那边的消息就好了. 外部的应 ...
- maven settings.xml 文件
指定jdk 的版本: <profile> <id>jdk-1.8</id> <activation> <activeByDefault>tr ...
- JDK静态代理示例代码
JDK静态代理示例代码 业务接口 接口的实现类 代理类,实现接口,并扩展实现类的功能 1.业务接口 /** * 业务接口 * @author pc * */ public interface User ...
- json的例子
{ "Code": 200, "Msg": "", "Result": "{\"Platfor ...
- 01-A-1: 计算