久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

Java Math類的三個(gè)方法ceil,floor,round用法

瀏覽:8日期: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入的計(jì)算,入的時(shí)候是到大于它的整數(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

測(cè)試程序如下:

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)); } }

運(yùn)行結(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

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

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 阿勒泰市| 昌图县| 嵊州市| 景宁| 七台河市| 霍邱县| 虎林市| 皋兰县| 平和县| 德安县| 迁安市| 黄龙县| 金华市| 凭祥市| 忻州市| 平武县| 中阳县| 许昌县| 平定县| 临清市| 昭觉县| 临洮县| 孝感市| 射洪县| 武冈市| 乐都县| 佛学| 双鸭山市| 林西县| 鹿泉市| 育儿| 泽普县| 文山县| 武隆县| 宜宾市| 桐柏县| 丰县| 皋兰县| 东辽县| 洛扎县| 上饶市|