Java基礎(chǔ)方法5(數(shù)字格式化)

5.1.數(shù)字格式化

import java.text.DecimalFormat;
public class DecimalFormatSimpleDemo{
    //使用實(shí)例化對(duì)象時(shí)設(shè)置格式化模式
    public static void SimpleFormat(String pattern,double value) {
    DecimalFormat myFormat = new DecimalFormat(pattern);
        //實(shí)例化DecimalFormat對(duì)象
    String output = myFormat.format(value);             
        //將數(shù)字進(jìn)行格式化
        System.out.println(value+" "+pattern+" "+output);
    }
    //使用applyPattern()方法對(duì)數(shù)字進(jìn)行格式化
    public static void UseApplyPatternMethodFormat(String pattern,double value) {
        DecimalFormat myFormat = new DecimalFormat();
        myFormat.applyPattern(pattern);
        System.out.println(value+" "+pattern+" "+myFormat.format(value));
    }
    public static void main(String[] args) {
        SimpleFormat("###,###,###", 123456.789);          
        //調(diào)用靜態(tài)SimpleFormat()方法
        SimpleFormat("00000000.###kg", 123456.789);        
        //在數(shù)字后面加上單位
        SimpleFormat("000000.000", 123.78);                 
       //按照格式模板格式化數(shù)字,不存在的位以0顯示
        //調(diào)用靜態(tài)UseApplyPatternMethodFormat()方法
        UseApplyPatternMethodFormat("#.###%", 0.789);       
        //將數(shù)字轉(zhuǎn)換為百分?jǐn)?shù)形式
        UseApplyPatternMethodFormat("###.##", 123456.789);  
        //將小數(shù)點(diǎn)后格式化為兩位
        UseApplyPatternMethodFormat("0.00\u2030", 0.789);   
        //將數(shù)字轉(zhuǎn)化為千分?jǐn)?shù)形式
    }
}

2.     DecimalFormat myFormat = new DecimalFormat();
        myFormat.setGroupingSize(2);
        String output = myFormat.format(123456.789);
        System.out.println("將數(shù)字以每兩個(gè)數(shù)字分組 "+output);
        myFormat.setGroupingUsed(false);
        String output2 = myFormat.format(123456.789);
        System.out.println("不允許數(shù)字分組 "+output2);

文集推薦:

Java基礎(chǔ)方法集1
Python基礎(chǔ)知識(shí)完整版
Spring Boot學(xué)習(xí)筆記
Linux指令進(jìn)階
Java高并發(fā)編程
SpringMVC基礎(chǔ)知識(shí)進(jìn)階
Mysql基礎(chǔ)知識(shí)完整版
健康管理系統(tǒng)學(xué)習(xí)花絮(學(xué)習(xí)記錄)
Node.js基礎(chǔ)知識(shí)(隨手筆記)
MongoDB基礎(chǔ)知識(shí)
Dubbo學(xué)習(xí)筆記
Vue學(xué)習(xí)筆記(隨手筆記)

聲明:發(fā)表此文是出于傳遞更多信息之目的。若有來源標(biāo)注錯(cuò)誤或侵犯了您的合法權(quán)益,請(qǐng)作者持權(quán)屬證明與本我們(QQ:981086665;郵箱:981086665@qq.com)聯(lián)系聯(lián)系,我們將及時(shí)更正、刪除,謝謝。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容