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 != ...
随机推荐
- MVC confirm提示
//审核不同意 $("#shbtg").click(function () { $.messager.confirm("提示", "不通过则会被删除, ...
- Spring Boot使用@Scheduled定时器任务
摘要: Spring Boot之使用@Scheduled定时器任务 假设我们已经搭建好了一个基于Spring Boot项目,首先我们要在Application中设置启用定时任务功能@EnableS ...
- mysql数据导入的时候提示Got a packet bigger than 'max_allowed_packet' bytes
Got a packet bigger than 'max_allowed_packet' bytes错误 默认可能是2M 把max_allowed_packet设置大于5M试试,我设置为160M,输 ...
- leetcode400
public class Solution { public int FindNthDigit(int n) { //StringBuilder sb = new StringBuilder(); / ...
- CDN理解<转>
CDN则是更高级的手段.CDN到底如何工作的呢,让我们来大概了解一下! CDN的基础百科资料也很多了,我也稍等提一下.CDN,Content Distribute Network,即:内容分发网络. ...
- UGUI 事件穿透规则
UGUI事件分为两大类:点击和拖拽. 点击包括 pointerdown, pointerup. 拖拽包括 begindrag, drag, enddrag. 点击事件无穿透:只会被最上层UI响应,不会 ...
- VS2008项目移植到Linux
不少人都遇到过这种情况:在Windows下用Visual Studio工具开发的程序需要移植到Linux系统中,做成Linux版本的,但程序比较大,在Linux上又离不开Make,手动编写Makefi ...
- consul service
{ "name": "consul-agent (host:{{ .MONITOR_CONSUL }})", "command&q ...
- 如何阅读jdk及开源框架的源码?
1.熟悉设计模式 可以边读源码 ,边熟悉设计模式,理解编程思想. jdk中对应的设计模式见:http://blog.csdn.net/gtuu0123/article/details/6114197 ...
- Linux C多线程实现生产者消费者
今天学习了用Linux C进行线程的同步,实现类似生产者消费者的问题.下面我就来分享我的代码 #include<stdio.h> #include<pthread.h> #in ...