1.把数字格式化为货币字符串:
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)); } }
2.格式化日期
package com.lixiyu; import java.util.Date; public class Year { public static void main(String[] args){ Date date=new Date(); String year=String.format("%tY",date); String month=String.format("%tB",date); String day=String.format("%td",date); System.out.println("今年是:"+year+"年"); System.out.println("现在是:"+month); System.out.println("今天是:"+day+"号"); } }