Java中常見(jiàn)的幾種四舍五入方法總結(jié)
在Java中,四舍五入到特定的小數(shù)位數(shù)是一個(gè)常見(jiàn)的需求,可以通過(guò)多種方式實(shí)現(xiàn)。以下是幾種常見(jiàn)的四舍五入方法及其代碼示例:
1. 使用Math.round()方法
Math.round()
方法可以將浮點(diǎn)數(shù)四舍五入到最接近的整數(shù)。如果你需要四舍五入到特定的小數(shù)位數(shù),可以先將數(shù)字乘以10的n次方(n為你想要保留的小數(shù)位數(shù)),然后使用Math.round()
進(jìn)行四舍五入,最后再除以10的n次方得到結(jié)果。
public class RoundExample { public static void main(String[] args) { double num = 3.14159; int decimalPlaces = 2; // 保留兩位小數(shù) double roundedNum = Math.round(num * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces); System.out.println(roundedNum); // 輸出 3.14 } }
2. 使用BigDecimal類
BigDecimal
類提供了更精確的浮點(diǎn)數(shù)運(yùn)算能力,包括四舍五入。它的setScale()
方法可以用來(lái)設(shè)置小數(shù)點(diǎn)后的位數(shù),并可以通過(guò)第二個(gè)參數(shù)指定舍入模式,例如BigDecimal.ROUND_HALF_UP
代表四舍五入。
import java.math.BigDecimal; import java.math.RoundingMode; public class BigDecimalRoundExample { public static void main(String[] args) { BigDecimal num = new BigDecimal("3.14159"); int decimalPlaces = 2; // 保留兩位小數(shù) BigDecimal roundedNum = num.setScale(decimalPlaces, RoundingMode.HALF_UP); System.out.println(roundedNum); // 輸出 3.14 } }
3. 使用String.format()方法
雖然String.format()
方法主要用于格式化字符串,但它也可以用于四舍五入浮點(diǎn)數(shù)到指定的小數(shù)位數(shù)。該方法不直接改變數(shù)字,而是將其格式化為包含指定小數(shù)位數(shù)的字符串。
public class StringFormatExample { public static void main(String[] args) { double num = 3.14159; String roundedNumStr = String.format("%.2f", num); double roundedNum = Double.parseDouble(roundedNumStr); // 如果需要再次作為double類型使用 System.out.println(roundedNum); // 輸出 3.14 } }
注意,使用String.format()
方法時(shí),結(jié)果是一個(gè)字符串,如果你需要將其作為浮點(diǎn)數(shù)進(jìn)行進(jìn)一步操作,可以使用Double.parseDouble()
將其轉(zhuǎn)換回double
類型。
4. 使用DecimalFormat類
DecimalFormat
是NumberFormat
的一個(gè)具體子類,用于格式化十進(jìn)制數(shù)。它允許你為數(shù)字、整數(shù)和小數(shù)指定模式。
import java.text.DecimalFormat; public class DecimalFormatExample { public static void main(String[] args) { double num = 3.14159; DecimalFormat df = new DecimalFormat("#.##"); // 保留兩位小數(shù) String roundedNumStr = df.format(num); double roundedNum = Double.parseDouble(roundedNumStr); // 如果需要再次作為double類型使用 System.out.println(roundedNum); // 輸出 3.14 } }
與String.format()
類似,DecimalFormat
的結(jié)果也是一個(gè)字符串,可以通過(guò)Double.parseDouble()
轉(zhuǎn)換回double
類型。
以上就是在Java中進(jìn)行四舍五入到特定小數(shù)位數(shù)的幾種常見(jiàn)方法。每種方法都有其適用場(chǎng)景,可以根據(jù)具體需求選擇使用。
總結(jié)
到此這篇關(guān)于Java中常見(jiàn)的幾種四舍五入方法的文章就介紹到這了,更多相關(guān)Java四舍五入方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
logback輸出日志屏蔽quartz的debug等級(jí)日志方式
這篇文章主要介紹了logback輸出日志屏蔽quartz的debug等級(jí)日志方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Springboot實(shí)現(xiàn)獲取實(shí)時(shí)天氣
這篇文章主要為大家詳細(xì)介紹了如何使用Springboot實(shí)現(xiàn)獲取實(shí)時(shí)天氣功能,文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04Springboot項(xiàng)目javax.validation使用方法詳解
這篇文章主要介紹了Springboot項(xiàng)目javax.validation使用方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04深入剖析Java中的synchronized關(guān)鍵字
在 Java 程序中,我們可以利用 synchronized 關(guān)鍵字來(lái)對(duì)程序進(jìn)行加鎖,它既可以用來(lái)聲明一個(gè) synchronized 代碼塊,也可以直接標(biāo)記靜態(tài)方法或者實(shí)例方法,本文就帶大家深入了解Java中的synchronized關(guān)鍵字,感興趣的同學(xué)可以參考閱讀2023-06-06Spring Boot 中的 Spring Cloud Feign的原
Spring Cloud Feign 是 Spring Cloud 中的一個(gè)組件,它可以幫助我們實(shí)現(xiàn)聲明式的 REST 客戶,這篇文章主要介紹了Spring Boot 中的 Spring Cloud Feign,需要的朋友可以參考下2023-07-07解決Java導(dǎo)入excel大量數(shù)據(jù)出現(xiàn)內(nèi)存溢出的問(wèn)題
今天小編就為大家分享一篇解決Java導(dǎo)入excel大量數(shù)據(jù)出現(xiàn)內(nèi)存溢出的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06