Java中Math.round()的用法及說(shuō)明
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)文章
使用Springboot實(shí)現(xiàn)獲取某個(gè)城市當(dāng)天的天氣預(yù)報(bào)
這篇文章主要為大家詳細(xì)介紹了使用Springboot實(shí)現(xiàn)獲取某個(gè)城市當(dāng)天的天氣預(yù)報(bào)的相關(guān)知識(shí),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04
java自定義任務(wù)類定時(shí)執(zhí)行任務(wù)示例 callable和future接口使用方法
Callable是類似于Runnable的接口,實(shí)現(xiàn)Callable接口的類和實(shí)現(xiàn)Runnable的類都是可被其它線程執(zhí)行的任務(wù)2014-01-01
Mybatis批量更新數(shù)據(jù)庫(kù)錯(cuò)誤問(wèn)題
這篇文章主要介紹了Mybatis批量更新數(shù)據(jù)庫(kù)錯(cuò)誤問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
利用SpringBoot實(shí)現(xiàn)多數(shù)據(jù)源的兩種方式總結(jié)
關(guān)于動(dòng)態(tài)數(shù)據(jù)源的切換的方案有很多,核心只有兩種,一種是構(gòu)建多套環(huán)境,另一種是基于spring原生的AbstractRoutingDataSource切換,這篇文章主要給大家介紹了關(guān)于利用SpringBoot實(shí)現(xiàn)多數(shù)據(jù)源的兩種方式,需要的朋友可以參考下2021-10-10
java中四種生成和解析XML文檔的方法詳解(介紹+優(yōu)缺點(diǎn)比較+示例)
本篇文章主要介紹了四種生成和解析XML文檔的方法,即:DOM、SAX、JDOM和DOM4J,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-11-11
利用SpringBoot解決多個(gè)定時(shí)任務(wù)阻塞的問(wèn)題
當(dāng)我們?cè)赟pring Boot應(yīng)用中使用多個(gè)定時(shí)任務(wù)時(shí),任務(wù)之間的阻塞可能是一個(gè)常見(jiàn)的問(wèn)題,這可能會(huì)因任務(wù)之間的依賴、執(zhí)行時(shí)間過(guò)長(zhǎng)或資源爭(zhēng)用等原因而發(fā)生,本文讓我們深入探討如何利用Spring Boot來(lái)解決多個(gè)定時(shí)任務(wù)阻塞的問(wèn)題,感興趣的小伙伴跟著小編一起來(lái)看看吧2024-01-01

