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

Java中常見的幾種四舍五入方法總結(jié)

 更新時(shí)間:2024年08月30日 10:41:40   作者:編程小郭  
在Java編程中四舍五入是一個(gè)常見的數(shù)學(xué)運(yùn)算需求,下面這篇文章主要給大家介紹了關(guān)于Java中常見的幾種四舍五入方法,文章通過代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

在Java中,四舍五入到特定的小數(shù)位數(shù)是一個(gè)常見的需求,可以通過多種方式實(shí)現(xià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()方法可以用來設(shè)置小數(shù)點(diǎn)后的位數(shù),并可以通過第二個(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類

DecimalFormatNumberFormat的一個(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è)字符串,可以通過Double.parseDouble()轉(zhuǎn)換回double類型。

以上就是在Java中進(jìn)行四舍五入到特定小數(shù)位數(shù)的幾種常見方法。每種方法都有其適用場(chǎng)景,可以根據(jù)具體需求選擇使用。

總結(jié)

到此這篇關(guān)于Java中常見的幾種四舍五入方法的文章就介紹到這了,更多相關(guān)Java四舍五入方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

定日县| 阜城县| 甘谷县| 吉安市| 瑞丽市| 黄陵县| 榆树市| 木里| 库车县| 平江县| 东阿县| 华阴市| 饶阳县| 瑞昌市| 鄯善县| 随州市| 房产| 恩平市| 宜兰县| 上林县| 阿拉善左旗| 永善县| 金平| 中卫市| 富川| 五原县| 鄂州市| 项城市| 郴州市| 长沙市| 丹东市| 康马县| 双城市| 大同县| 松桃| 邵阳县| 沁源县| 正安县| 天门市| 玉屏| 五莲县|