avatar
int --> String?# Java - 爪哇娇娃
k*o
1
how to transform int 1000 to "1,000" instead of "1000"?
avatar
c*y
2
You can write an toComaString function to do that easily.
This is one I came up:
public static String toCommaString(int i){
String s=String.valueOf(i);
int l=s.length();
if(l <= 3) return s;
StringBuffer sb=new StringBuffer(s);
int ll=l-3;
while (ll>0){
sb.insert(ll,',');
ll=ll-3;
}
return sb.toString();
}

【在 k*o 的大作中提到】
: how to transform int 1000 to "1,000" instead of "1000"?
avatar
d*p
3
java.txt.DecimalFormat can do it and much more.

【在 k*o 的大作中提到】
: how to transform int 1000 to "1,000" instead of "1000"?
avatar
KG
4
A better way to do it is to use NumberFormat, taking into account the Locale.

【在 c*y 的大作中提到】
: You can write an toComaString function to do that easily.
: This is one I came up:
: public static String toCommaString(int i){
: String s=String.valueOf(i);
: int l=s.length();
: if(l <= 3) return s;
: StringBuffer sb=new StringBuffer(s);
: int ll=l-3;
: while (ll>0){
: sb.insert(ll,',');

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。