最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2021年07月06日 09:09:50   作者:ple嬸  
這篇文章主要介紹了Java Math類的三個(gè)方法ceil,floor,round用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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),即將原來(lái)的數(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.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

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

许昌县| 扎鲁特旗| 宣化县| 招远市| 峨山| 木里| 博客| 瑞丽市| 泸定县| 湾仔区| 都匀市| 四子王旗| 山西省| 新野县| 宣化县| 融水| 长海县| 柞水县| 七台河市| 利川市| 涟源市| 岱山县| 英山县| 定边县| 琼结县| 泰安市| 福海县| 衡阳县| 岳西县| 原平市| 墨玉县| 宜昌市| 日照市| 万盛区| 永和县| 咸丰县| 鹤庆县| 霍城县| 卢龙县| 鄂州市| 会同县|