发表于: java/j2ee | 作者: | 日期: 2015/6/22 03:06

java开发中经常会有数字、货币金钱等格式化需求,货币保留几位小数,货币前端需要加上货币符号等。可以用java.text.NumberFormat和java.text.DecimalFormat实现。
第一种:比如网上交易系统,数字保留4位小数:

public static void main(String[] args){
NumberFormat nf = new DecimalFormat(“##.####”);
Double d = 554545.4545454;
String str = nf.format(d);
System.out.println(str);
//输出554545.4545
}

第二种:比如网上交易系统,金钱数字保留4位小数且以“¥”开头:

public static void main(String[] args){
NumberFormat nf = new DecimalFormat(“$##.####”);
Double d = 554545.4545454;
String str = nf.format(d);
System.out.println(str);
//$554545.4545
}

第三种:比如网上交易系统,金钱数字保留4位小数且三位三位的隔开:

public static void main(String[] args){
NumberFormat nf = new DecimalFormat(“#,###.####”);
Double d = 554545.4545454;
String str = nf.format(d);
System.out.println(str);
//554,545.4544;
}

[整理自网络]

: https://blog.darkmi.com/2015/06/22/2816.html

本文相关评论 - 1条评论都没有呢
Post a comment now » 本文目前不可评论

No comments yet.

Sorry, the comment form is closed at this time.