383
技術社區[雲棲]
把數字格式化為貨幣字符串 NumberFormat
package com.chapter.four.one.cn; import java.text.NumberFormat; import java.util.Locale; import java.util.Scanner; public class FormatNumber { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("請輸入一個數字"); double number = scan.nextDouble();//獲取用戶輸入數字 System.out.println("該數字用Locale類的以下常量作為格式化對象的構造參數,將獲得不同的貨幣格式:"); //創建格式化對象 NumberFormat format = NumberFormat.getCurrencyInstance(Locale.CHINA); //輸出格式化貨幣格式 System.out.println("Locale.CHINA: "+format.format(number)); format = NumberFormat.getCurrencyInstance(Locale.US); System.out.println("Locale.US:" + format.format(number)); format = NumberFormat.getCurrencyInstance(Locale.ENGLISH); System.out.println("Locale.ENGLISH:" + format.format(number)); format = NumberFormat.getCurrencyInstance(Locale.TAIWAN); System.out.println("Locale.TAIWAN:" + format.format(number)); } }
輸出:
請輸入一個數字
5
該數字用Locale類的以下常量作為格式化對象的構造參數,將獲得不同的貨幣格式:
Locale.CHINA: ¥5.00
Locale.US:$5.00
Locale.ENGLISH:¤5.00
Locale.TAIWAN:NT$5.00
最後更新:2017-04-02 17:09:26