public static String addComma(String str) {
if(!TextUtils.isEmpty(str)){
String front = str.substring(0, str.length() - 3);
String behind = str.substring(str.length() - 3, str.length());
// 将传进数字反转
String reverseStr = new StringBuilder(front).reverse().toString();
String strTemp = "";
for (int i = 0; i < reverseStr.length(); i++) {
if (i * 3 + 3 > reverseStr.length()) {
strTemp += reverseStr.substring(i * 3, reverseStr.length());
break;
}
strTemp += reverseStr.substring(i * 3, i * 3 + 3) + ",";
}
// 将[789,456,] 中最后一个[,]去除
if (strTemp.endsWith(",")) {
strTemp = strTemp.substring(0, strTemp.length() - 1);
}
// 将数字重新反转
String resultStr = new StringBuilder(strTemp).reverse().append(behind).toString();
return resultStr;
}
return str;
}
转载于:https://my.oschina.net/meetthebetteryour/blog/607877