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

Java中Math.round()的用法及說明

 更新時間:2024年02月24日 15:38:46   作者:木木是木木  
這篇文章主要介紹了Java中Math.round()的用法及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

Math.round()的用法

遇到了關于Math.round()的用法的基礎題,發(fā)現(xiàn)自己還不是太熟悉,所以來總結一下。

Java中的Math.round()方法是將浮點型進行“四舍五入”轉換為int類型的一個方法。

使用細節(jié)可以看例題

  • 小數(shù)點后第一位等于五時:
System.out.println(Math.round(-11.5)); -> 輸出為 -11
System.out.println(Math.round(11.5)); -> 輸出為 12
  • 小數(shù)點后第一位小于五時:
System.out.println(Math.round(-11.41)); -> 輸出為 -11
System.out.println(Math.round(11.41)); -> 輸出為 11
  • 小數(shù)點后第一位大于五時:
System.out.println(Math.round(-11.58)); -> 輸出為 -12
System.out.println(Math.round(11.58)); -> 輸出為 12

代碼驗證

public class main {
    public static void main(String[] args) {

        System.out.println(Math.round(-11.5));
        System.out.println(Math.round(11.5));

        System.out.println(Math.round(-11.41));
        System.out.println(Math.round(11.41));

        System.out.println(Math.round(-11.58));
        System.out.println(Math.round(11.58));
    }
}

結果圖:

一句話結論:

將括號內(nèi)的數(shù) + 0.5 向下取整即為輸出。

驗證結論

  • 小數(shù)點后第一位等于五時:
System.out.println(Math.round(-11.5)); -> -11.5 + 0.5 = -11 向下取整輸出為 -11
System.out.println(Math.round(11.5)); -> 11.5 + 0.5 = 12 向下取整輸出為 12
  • 小數(shù)點后第一位小于五時:
System.out.println(Math.round(-11.41)); -> -11.41 + 0.5 = -10.91 向下取整輸出為 -11
System.out.println(Math.round(11.41)); -> 11.41 + 0.5 = 11.91 向下取整輸出為 11
  • 小數(shù)點后第一位大于五時:
System.out.println(Math.round(-11.58)); -> -11.58 + 0.5 = -11.08 向下取整輸出為 -12
System.out.println(Math.round(11.58)); -> 11.58 + 0.5 = 12.05 向下取整輸出為 12

總結

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

相關文章

最新評論

平罗县| 依兰县| 高邑县| 星子县| 宣武区| 新乐市| 和政县| 贵阳市| 河曲县| 鹤峰县| 阳高县| 白水县| 商河县| 高青县| 铁力市| 府谷县| 三门县| 万山特区| 海原县| 本溪市| 固安县| 平果县| 高邮市| 镇原县| 西城区| 双峰县| 甘谷县| 库尔勒市| 城步| 沁源县| 格尔木市| 白玉县| 蓬安县| 信阳市| 鄂尔多斯市| 菏泽市| 竹溪县| 郎溪县| 阜康市| 平南县| 南康市|