国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Java Math類的三個方法ceil,floor,round用法

瀏覽:4日期:2022-08-09 11:00:48
Math類的ceil,floor,round用法ceil()方法

就表示向上取整,Math.ceil(12.3)的結(jié)果是13,Math.ceil(-12.7)的結(jié)果-12;

floor()方法

就表示向下取整,Math.floor(12.7)的結(jié)果是12,Math.floor(-12.3)的結(jié)果-13;

round()方法

表示“四舍五入”,Math.round(12.3)的結(jié)果是12,Math.round(-12.7)的結(jié)果-13;

Math的 floor,round和ceil總結(jié)

floor 返回不大于的最大整數(shù)

round 則是4舍5入的計算,入的時候是到大于它的整數(shù)

round方法,它表示“四舍五入”,算法為Math.floor(x+0.5),即將原來的數(shù)字加上0.5后再向下取整,所以,Math.round(11.5)的結(jié)果為12,Math.round(-11.5)的結(jié)果為-11。

ceil 則是不小于他的最小整數(shù)

看例子 Math.floor Math.round Math.ceil 1.4 1 1 2 1.5 1 2 2 1.6 1 2 2 -1.4 -2 -1 -1 -1.5 -2 -1 -1 -1.6 -2 -2 -1

測試程序如下:

public class MyTest { public static void main(String[] args) { double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 }; for (double num : nums) { test(num); } } private static void test(double num) { System.out.println('Math.floor(' + num + ')=' + Math.floor(num)); System.out.println('Math.round(' + num + ')=' + Math.round(num)); System.out.println('Math.ceil(' + num + ')=' + Math.ceil(num)); } }

運行結(jié)果

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

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 枝江市| 百色市| 喀喇| 黄骅市| 肥东县| 连州市| 江口县| 凤翔县| 临夏市| 南城县| 景德镇市| 拉孜县| 东山县| 宜川县| 敦化市| 利辛县| 格尔木市| 政和县| 潜山县| 英山县| 安西县| 黄浦区| 新丰县| 黔江区| 南部县| 柞水县| 司法| 永昌县| 连平县| 吴江市| 徐汇区| 堆龙德庆县| 浏阳市| 邵阳市| 兴海县| 梨树县| 镇雄县| 西盟| 大港区| 静宁县| 科尔|