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 != ...
随机推荐
- Vue 基本用法
Vue的基本用法 模板语法{{ }} 关闭掉 django中提供的模板语法{{ }} 指令系统 v-text v-html v-show和v-if v-bind和v-on v-for v-model ...
- app测试笔记记录
1. 個性簽名保存成功,toast提示“儲存成功” 解释: Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过 ...
- Bootstrap-CL:分页
ylbtech-Bootstrap-CL:分页 1.返回顶部 1. Bootstrap 分页 本章将讲解 Bootstrap 支持的分页特性.分页(Pagination),是一种无序列表,Bootst ...
- istio 配置https gateway
沒有親手實驗,参考官方文档: https://istio.io/docs/tasks/traffic-management/secure-ingress/
- VCF文件导入导出
参考资料 通讯录导入导出vcf格式文件方法可参考: https://qiaodahai.com/android-iphone-mobile-phones-contacts-import-and-exp ...
- django-常用过滤器
django常用过滤器 add :字符串相加,数字相加,列表相加,如果失败,将会返回一个空字符串. default:提供一个默认值,在这个值被django认为是False的时候使用.比如:空字符串.N ...
- uwsgi的使用
uwsgi是一个WEB服务器,只要用于python部分,类似于nginx ,apache 1 使用pip命令安装 pip install uwsgi 安装成功以后 可以做一个简单的测试 2 新建一个t ...
- JAVA获取txt文件内容
JAVA 读取txt文件内容 通常,我们可以直接通过文件流来读取txt文件的内容,但有时可能会出现乱码!此时只要设置一下文件字符编码即可. public class txttest { /** * 读 ...
- ScheduledThreadPoolExecutor 线程池调度 使用
package other; import java.util.concurrent.Callable; import java.util.concurrent.Executors; import j ...
- Xpath解析xml
Xpath解析xml其实最主要的是查找xml文档中信息,而且不需要了解xml文档结构 package com.huawei.xml; import java.io.InputStream;import ...