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 != ...
随机推荐
- struts2学习(1)struts2 helloWorld
一.struts2简介: 二.helloWorld: 1)工程结构: HelloWorldAction.java: package com.cy.action; import com.opensymp ...
- HDU 3746 Cyclic Nacklace (用kmp求循环节)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- web开发的功能备忘录
总体设计 Web项目开发中,我们可以根据不同的功能,将整个系统的功能进行划分.系统功能总体来说可以划分为两个模块:系统的必备功能和逻辑业务功能. 下面主要是分析一下系统的必备功能,所谓系统的必备功能是 ...
- JavaScript(二)-精简
十三 DOM(文档对象模型) 1 作用 : 可访问javascript HTML文档的所有元素. 2 功能: (1) 改变html输出流 eg: <script> ...
- delete删除属性
/* 删除实例属性 */ function MyObject() { this.name = "我是实例的name"; } var obj = new MyObject(); al ...
- Thymeleaf系列五 迭代,if,switch语法
1. 概述 这里介绍thymeleaf的编程语法,本节主要包括如下内容 迭代语法:th:each; iteration status 条件语法:th:if; th:unless switch语法: ...
- Spring框架中的单例Beans是线程安全的么
Spring框架并没有对单例bean进行任何多线程的封装处理.关于单例bean的线程安全和并发问题需要开发者自行去搞定.但实际上,大部分的Spring bean并没有可变的状态(比如Serview类和 ...
- CUDA 新版本 Visual Studio 和 CUDA 兼容性的小问题
▶ 升级到 Visual Studio 2017 和 CUDA 9.1 之后,直接编译以前的 CUDA C 程序出现了如下报错: 严重性 代码 说明 项目 文件 行 禁止显示状态 错误(活动) E00 ...
- Windows下测量cpu利用率的类
方法参考网上的一段代码,把它改成了类以方便调用,很简单,但是可以一用,哈哈. cpu_usage.h #include <windows.h> class CpuUsage { publi ...
- 【C#】串口操作实用类
做工业通 信有很长时间了,特别是串口(232/485),有VB/VC/C各种版本的串口操作代码,这些代码也经过了多年的现场考验,应该说是比较健壮的代码,但 是目前却没有C#相对成熟的串口操作代码,最近 ...