numberformatdemo.java

来自「本java源程序包括了大量的学习程序(共27章)方便大家学习」· Java 代码 · 共 42 行

JAVA
42
字号
import java.text.*;
import java.util.Date;
import java.util.Locale;

public class NumberFormatDemo 
{
	//显示格式化货币
	public void showMoney(double money, Locale locale)
	{
		//用于按照指定地区规则,格式化货币数据信息并输出
		NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
		String str = numberFormat.format(money);
		System.out.println("The Money is: " + str);
	}
	
	//显示格式化数字
	public void showNumber(double num, Locale locale)
	{
		//用于按照指定地区规则,格式化数字信息并显示
		NumberFormat numberFormat = NumberFormat.getIntegerInstance(locale);
		numberFormat.setMaximumFractionDigits(5);
		String str = numberFormat.format(num);
		System.out.println("The Number is: " + str);
	}	
	
	public static void main(String[] args) 
	{
		NumberFormatDemo demo = new NumberFormatDemo();
		double money = 3000.00;
		double number = 1000.0/3.0;
		//打印中国格式的货币和数字
		System.out.println("中国:");
		demo.showMoney(money,Locale.CHINA);
		demo.showNumber(number,Locale.CHINA);
		//打印美国格式的货币和数字
		System.out.println("美国:");
		demo.showMoney(money,Locale.US);
		demo.showNumber(number,Locale.US);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?