Math的 floor,round和ceil的总结

parseInt()、Math.round(),Math.ceil(),Math.floor()的区别 最近看算法程序时,出现了这几种取整函数,整理出来加以区分。 1、parseInt() 该方法取整是把小数点后面小数去掉,只保留整数部分。如果要取整的数为正时,类似Math.floor();为负时,类似Math.ceil() 那么问题来了,Math.floor()、Math.ceil()怎么用呢? 2、Math.ceil() “向上取整”, 即小数部分直接舍去,并向正数部分进1 double... 阅读详情
floor 返回不大于的最大整数
found 则是4舍5入的计算,入的时候是到大于它的整数
ceil 则是不小于他的最小整数

看例子


Math.floor Math.round Math.ceil
1.4 11
2
1.5 12
2
1.6 12
2
-1.4 -2-1
-1
-1.5 -2-1
-1
-1.6 -2-2
-1

测试程序如下:

  1. public class MyTest {
  2.   public static void main(String[] args) {
  3.     double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 };
  4.     for (double num : nums) {
  5.       test(num);
  6.     }
  7.   }
  8.   private static void test(double num) {
  9.     System.out.println("Math.floor(" + num + ")=" + Math.floor(num));
  10.     System.out.println("Math.round(" + num + ")=" + Math.round(num));
  11.     System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num));
  12.   }
  13. }


运行结果
Math.floor(1.4)=1.0
Math.round(1.4)=1
Math.ceil(1.4)=2.0
Math.floor(1.5)=1.0
Math.round(1.5)=2
Math.ceil(1.5)=2.0
Math.floor(1.6)=1.0
Math.round(1.6)=2
Math.ceil(1.6)=2.0
Math.floor(-1.4)=-2.0
Math.round(-1.4)=-1
Math.ceil(-1.4)=-1.0
Math.floor(-1.5)=-2.0
Math.round(-1.5)=-1
Math.ceil(-1.5)=-1.0
Math.floor(-1.6)=-2.0
Math.round(-1.6)=-2
Math.ceil(-1.6)=-1.0










<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
Math.ceil与Math.floor与Math.round 1.Math.ceil() 向上取整 //Math.ceil 向上取整 double ceil1 = Math.ceil(12.13);//13.0 double ceil2 = Math.ceil(12.5);//13.0 double ceil3 = Math.ceil(12.63);//13.0 double ceil4 = Math.ceil(-12.13);//-12.0 double ceil5. 阅读详情

相关推荐

深入解读ATPG Pattern类型:除了Basic Scan,Clock PO和RAM Sequential模式怎么用?

本文深入解析ATPG高级Pattern类型,包括Clock PO和RAM Sequential模式的应用策略。通过Mentor DFT工具链中的Tessent平台,详细介绍了这些模式的工作原理、配置命令及实战案例,帮助提升芯片测试的故障覆盖率和效率。特别适用于处理时钟域交叉和嵌入式存储器的复杂电路测试需求。

weixin_27697385的博客 493

java math方法

math方法(小数点两位数s四舍五入) math.ceil() 向上取整 math.floor() 向下取整 math.found() 四舍五入 保留两位小数四舍五入 double dd = Math.round(d100)/100.0; //整数,任何数与整数算都是整数,double dd = Math.round(d100)*0.01; //错误,double的特性输出的答案错误 system.out.println(dd); ...

weixin_45003321的博客 169

TP4054充电电路发热严重?手把手教你调整电阻值降低温度(附实测数据)

本文针对TP4054锂电池充电电路发热严重的问题,提供了详细的排错与解决方案。通过分析发热原理,指出核心在于PROG引脚电阻值设定,并手把手指导如何计算和选择正确的电阻以降低充电电流与温度。文末附有不同电阻下的实测温度与性能数据,为电子爱好者和工程师提供了实用的调试参考。

p4q5r6s7t的博客 409

Math.round()方法

Math.round方法举例: Case1:小数点后第一位 = 5 正数:Math.round(11.5) = 12 负数:Math.round(-11.5) = -11 Case2:小数点后第一位 < 5 正数:Math.round(11.49) = 11 负数:Math.round(-11.49) = -1...

零落凉辰@的专栏 4万+

从零开始:Basler相机与pylon Viewer的工业视觉入门指南

本文详细介绍了Basler相机与pylon Viewer在工业视觉系统中的入门指南,涵盖硬件连接、软件安装、核心功能解析及实战案例。通过pylon Viewer的全面控制和参数调整功能,帮助新手快速掌握工业视觉检测技术,提升生产效率。

julia4scientist的博客 1144

TEC半导体制冷片避坑指南:散热、电源与PID温控实战

半导体制冷片基于帕尔帖效应,通过直流电实现冷热面热量搬运,具备无运动部件、控温精度高的特点,但效率低、发热量大,对散热和电源要求苛刻。其技术价值在于小空间高精度温控,广泛应用于激光器、PCR仪、恒温箱等场景。然而,散热设计不良会导致冷面不冷,电源功率不足或纹波干扰会引发电流不稳,PID参数整定不当则造成温度震荡。此外,传感器位置、冷凝水防护和热循环应力也直接影响系统稳定性与寿命。本文结合万用表实测、散热选型与PID调参经验,系统梳理TEC应用中的典型问题与解决思路。

weixin_29191081的博客 307

word2016提示mathtype文件未找到:MathPage.wll

word2016提示mathtype文件未找到:MathPage.wll 解决office2016中的word2016的mathtype无法使用的问题,这类问题通常体现为,打开word后提示“未找到文件:mathtype.wll”,或者错误53等。 工具/原料 word2016 mathtype 方法/步骤 步骤1 先安装word,再安装mathtype,然后在路径C:\Program Files (x86)\MathType\Office Support\64(如果自定义安装路径的话请选择对应的路径,32

平平淡淡,戒急用忍,一生学闭嘴。 2718

编译原理2024期末复习(面向做题,大题速学)【太原理工大学】

归约-归约”冲突就是在项目集规范族一个状态中两个。

m0_63181421的博客 1万+

Java中Math.round()的取整规则

做Java的面试题时遇到了以下这题,百度了一下Math.round()的修约规则,有的说是四舍五入,有的说是四舍六入,发现和我学分析化学时用的数字修约规则(四舍六入五成双)很像,所以验证一下; 原题:Math.round(11.5) 等于多少?Math.round(-11.5)等于多少? 作者给的解题方法如下: 答:Math.round(11.5)的返回值是12,Math.round(-11.

shrub的博客 20万+

STM32Cube高效开发教程<基础篇>(一)----概述

一、 STM32系列器件和开发工具发展历史声明:本人水平有限,博客可能存在部分错误的地方,请广大读者谅解并向本人反馈错误。本专栏博客参考《STM32Cube高效开发教程(基础篇)》,有意向的读者可以购买正版书籍辅助学习,本书籍由王维波老师、鄢志丹老师、王钊老师倾力打造,书籍内容干货满满。一、 STM32系列器件和开发工具发展历史。

我的博客 195

CryoEM - 使用 ChimeraX 工具 操作 冷冻电镜的 MRC 密度图(Density Map) 教程

由冷冻电镜 CryoSparc 软件所生成的 Volume,需要观察其内部结构,建议使用 ChimeraX 进行操作。

AGI 4974

Math.round(),Math.ceil(),Math.floor()的区别

1、Math.round() “四舍五入”,该函数返回的是一个四舍五入后的的整数 double d = 3.1415926; double d2 = 18.58; double d3 = -15.23; double d4 = -16.85; long round1 = Math.round(d); //...

zuihongyan518的博客 22万+

RK3568-适配phy芯片ip101gri

复位时间查阅数据手册。

Paper_Love的博客 2651

Math ceil()、floor()、round()方法

HI,我是小瑞! 7万+

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Java,Math类中的ceil、floor和round函数源码解析以及自己重写实现

1. ceil、floor和round的功能 首先,这三个方法都是Math类的静态方法,而且类Math在java.lang包下,所以我们在代码中可以直接调用Math的方法。 Math.ceil(double a)实现的是对小数向右取整,如 Math.ceil(-0.7) = -0.0,Math.ceil(0.5) = 1.0, Math.ceil(1.3) = 2.0 Math.floor(double a)实现的是对小数向左取整,如 Math.floor(-0.7) = -1.0,Math.floo

走过路过不要错过 3817

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

Math 的 ceil、floor、round方法详解及示例

Math 的 ceil、floor、round方法详解及示例

韩金群的博客 1万+

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

Math ceil()、floor()、round()三者的区别

Math ceil、floor、round三者的区别实例英文单词结论前言一、pandas是什么?二、使用步骤1.引入库2.读入数据总结 实例 先看一个正数例子 System.out.println(Math.ceil(11.1f)); //12.0 System.out.println(Math.ceil(11.6f)); //12.0 System.out.println(Math.round(11.1f)); //11 System.out.println(Math.

一枚程序猿 1328

Math类四个常用方法辨析,floor、ceil、round、rint

Math.floorfloor,英文原意:地板。 Math.floor 函数是求一个浮点数的地板,就是 向下 求一个最接近它的整数,它的 值肯定会小于或等于这个浮点数。再看下面的例子的时候,脑中想象一个竖着的数轴,负数在下,正数在上。这样对于向上取整和向下取整的说法,可能会更容易理解。例子: System.out.println("Math.floor(-1.1): " + Mat

自己走的路 1万+

Java Math的 floor,round和ceil的总结

floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数(当-1.5时可见,四舍五入后得到的结果不是我们期待的,解决办法是先对他取绝对值,然后在用round方法)round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果为12,Math.round(-11.5

foart的专栏 19万+

【Java】Math.round(),Math.ceil(),Math.floor()的区别

Math.round() “四舍五入”, double d = 3.1415926; double d2 = 18.58; double d3 = -15.23; double d4 = -16.85; long round1 = Math.round(d); // 结果 3 long round2 = Math.round(d2); // 结果 19 long round3 = Ma

瑞新の博客:bennyrhys 1745

vue中math.ceil_JavaScript中的Math Ceil Math Round和Math Floor

vue中math.ceil“The universe is regulated by Math formulas” my grumpy teacher used to say. And the galaxy of coding isn’t escaping from that rule any time soon — as I was hoping. Luckily for us, a langu...

weixin_26645549的博客 1566

math.floor java_Java Math floor、ceil、rint 及 round 用法

floor 返回不大于的最大整数。round 则是 4 舍 5 入的计算,入的时候是到大于它的整数。round 表示"四舍五入",算法为Math.floor(x+0.5) ,即将原来的数字加上 0.5 后再向下取整,所以 Math.round(11.5) 的结果为 12,Math.round(-11.5) 的结果为 -11。我们先通过以下实例来看看 Java Math floor、ceil、ri...

weixin_36332616的博客 1037

JS中Math之random,round,ceil,floor的用法总结

1.Math.random(); 结果为0-1间的一个随机数(包括0,不包括1)    Math.random(); 结果为0-1间的一个随机数(包括0,不包括1) [html] view plaincopyprint?    2.Math.floor(num); 参数num为一个数值,函数结果为num的整数部分(返回小于等于n的最大整数)。  Math.floor(num);

hpu_jxust的专栏 2604

java 中Math的ceil、floor、round取整方法

java 中Math的ceil、floor、round取整方法

m0_48170265的博客 652
上一篇: 致职场新人的几点建议
下一篇: 对字符串中字符出现的次数进行排序
老紫竹
博客等级 码龄19年 1万+粉丝 1252原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址