第一种用程序控制
/**
* 根据特点字符来分割字符串
* str: 要分割的字符串
* type:分隔符
* t:每隔t数量来分割字符串str
* 如str: 1324545334
* stype:,
* t:3
* 结果为:1,324,545,334
* @exception
* @author:Zhanglinxing
* @since 1.0.0
*/
public static String getStringByType(String str,String type,int t){
String result=""; //要返回的字符串结果
if(str.length() <= t){
return str;
}else{
str = new StringBuilder(str).reverse().toString(); //先将字符串颠倒顺序
for(int i=0;i<str.length();i++){
if(i*t+t > str.length()){
result += str.substring(i*t, str.length());
break;
}
result += str.substring(i*t, i*t+t)+type;
}
if(result.endsWith(type)){
result = result.substring(0, result.length()-1);
}
}
return new StringBuilder(result).reverse().toString();
}
第二种DecimalFormat 格式化
BigDecimal a=new BigDecimal("233333333333333333333333333");
DecimalFormat df=new DecimalFormat(",###,##0"); //没有小数
DecimalFormat df=new DecimalFormat(",###,##0.0"); //保留一位小数
df.format(a);
System.out.println(df.format(a));
用千分位格式化数据“123456789”
最新推荐文章于 2024-02-19 18:49:22 发布