将数字格式化成货币格式

package my;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Scanner;
public class number {
    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));
    }
}

效果如下:

将数字格式化成货币格式_第1张图片

你可能感兴趣的:(JAVA)