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

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

Java Math.round函數(shù)詳解

瀏覽:45日期:2023-02-12 15:43:15

1.代碼如下:

public class TestMathRound { public static void main(String[] args) {System.out.println('小數(shù)點(diǎn)后第一位=5');System.out.println('正數(shù):Math.round(11.5)=' + Math.round(11.5));//12System.out.println('負(fù)數(shù):Math.round(-11.5)=' + Math.round(-11.5));//-11System.out.println();System.out.println('小數(shù)點(diǎn)后第一位<5');System.out.println('正數(shù):Math.round(11.46)=' + Math.round(11.46));//11System.out.println('負(fù)數(shù):Math.round(-11.46)=' + Math.round(-11.46));//-11System.out.println();System.out.println('小數(shù)點(diǎn)后第一位>5');System.out.println('正數(shù):Math.round(11.68)=' + Math.round(11.68));//12System.out.println('負(fù)數(shù):Math.round(-11.68)=' + Math.round(-11.68));//-12 }}

2.結(jié)果如下,可以自己運(yùn)行。

Java Math.round函數(shù)詳解

3.本來(lái)以為是四舍五入,取最靠近的整數(shù),查了網(wǎng)上說(shuō)有四舍六入五成雙,最后還不如看源碼。源碼如下:

public static long round(double a) {if (a != 0x1.fffffffffffffp-2) // greatest double value less than 0.5 return (long)floor(a + 0.5d);else return 0; }

 我們看到round函數(shù)會(huì)默認(rèn)加0.5,之后調(diào)用floor函數(shù),然后返回。floor函數(shù)可以理解為向下取整。

Java Math.round函數(shù)詳解

4.綜上,Math.round函數(shù)是默認(rèn)加上0.5之后,向下取整。

到此這篇關(guān)于Java Math.round函數(shù)詳解的文章就介紹到這了,更多相關(guān)Java Math.round函數(shù)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 海伦市| 山阳县| 定州市| 上饶市| 察隅县| 乐都县| 双流县| 张家口市| 安康市| 贺兰县| 诸暨市| 葫芦岛市| 沈阳市| 福建省| 玉龙| 长兴县| 神池县| 新竹县| 邹城市| 夏河县| 仁化县| 尤溪县| 安福县| 英吉沙县| 安乡县| 邢台县| 麻阳| 隆林| 德惠市| 阿克陶县| 邓州市| 平阴县| 九台市| 合江县| 班戈县| 滨海县| 徐汇区| 蓬溪县| 霍州市| 巴楚县| 新和县|