日期:2014-05-20 浏览次数:20887 次
public static void main(String[] args) {
String num = "-1232354234.1254215325";
System.out.println(convert(num));
}
public static String convert(String num) {
if (num == null
------解决方案--------------------
!num.matches("\\-?\\d+\\.?\\d+"))
throw new IllegalArgumentException("number is invalid");
int index = num.indexOf(".");
int end = num.startsWith("-") ? 1 : 0;
StringBuilder temp = new StringBuilder(num);
for (int i = (index == -1 ? temp.length() : index) - 3; i > end; i -= 3) {
temp.insert(i, ',');
}
return temp.toString();
}
NumberFormat nf = new DecimalFormat("###,###.##");
String[] price = new String[3];
price[0] = "23445.78";
price[1] = "34323445.78";
price[2] = "234323445.78";
for(String str : price){
System.out.println(nf.format(Double.valueOf(str)));;
}