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

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

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

Math.round()的用法

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

Java中的Math.round()方法是將浮點(diǎn)型進(jìn)行“四舍五入”轉(zhuǎn)換為int類型的一個(gè)方法。

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

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

代碼驗(yàn)證

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

結(jié)果圖:

一句話結(jié)論:

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

驗(yàn)證結(jié)論

  • 小數(shù)點(diǎn)后第一位等于五時(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ù)點(diǎn)后第一位小于五時(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ù)點(diǎn)后第一位大于五時(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

總結(jié)

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

相關(guān)文章

最新評(píng)論

马尔康县| 新津县| 定兴县| 克什克腾旗| 淮北市| 云林县| 厦门市| 武汉市| 喜德县| 永仁县| 长寿区| 汶上县| 云南省| SHOW| 霍邱县| 兰溪市| 任丘市| 怀安县| 同德县| 屏南县| 大荔县| 英吉沙县| 东丰县| 延川县| 双辽市| 勐海县| 航空| 洛宁县| 平安县| 中卫市| 教育| 延安市| 房山区| 广平县| 潼南县| 通辽市| 泰顺县| 民乐县| 邵东县| 虹口区| 鄱阳县|