java格式化金额数字

阅读更多
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class TestFormatter {

	public static void main(String[] args) {
		String str = "0600450625465.5626";
		TestFormatter t = new TestFormatter();
		System.out.println(t.getFormatter(str));
		System.out.println(t.getCurrency(str));
		System.out.println(t.getDecimalFormat(str));
	}

	private String getFormatter(String str) {
		NumberFormat n = NumberFormat.getNumberInstance();
		double d;
		String outStr = null;
		try {
			 d = Double.parseDouble(str);
			 outStr = n.format(d);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return outStr;
	}
	
	private String getDecimalFormat(String str){
		DecimalFormat   fmt   =   new   DecimalFormat("##,###,###,###,##0.00000");  
		String outStr = null;
		double d;
		try {
			d = Double.parseDouble(str);
			outStr = fmt.format(d);
		} catch (Exception e) {
		}
		return outStr;
	}

	private String getCurrency(String str) {
		NumberFormat n = NumberFormat.getCurrencyInstance();
		double d;
		String outStr = null;
		try {
			 d = Double.parseDouble(str);
			 outStr = n.format(d);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return outStr;
	}
}


测试结果
600,450,625,465.563
¥600,450,625,466
600,450,625,465.56260

你可能感兴趣的:(Java)