日期:2014-05-20  浏览次数:20719 次

急,急,急!!!在字符串中添加分隔符
有一数字字符串,如3123726500.00     要让它转化成货币类型的表示方式,即转化成
3,123,726,500.00  

求简单的转化算法

谢谢各位了~~~~~~

------解决方案--------------------
方法一:不用正则表达式:
public static fianl String COM = ", ";
public static String commaNumber(String src) {
if (isNull(src)) {
return BLANK;
}

boolean negativeOk = false;
boolean syousuu = false;
String srcleft = null;
String srcright = null;
if (!isNull(src)) {
if (0 < src.indexOf( ". ")) {
syousuu = true;
srcleft = src.substring(0, src.indexOf( ". "));
srcright = src.substring(src.indexOf( ". "), src.length());
} else {
srcleft = src;
}
if (srcleft.startsWith( "- ")) {
negativeOk = true;
srcleft = srcleft.substring(1);
}
while (srcleft.startsWith( "0 ") && 1 < srcleft.length()) {
srcleft = srcleft.substring(1);
}
final int len = srcleft.length();
for (int i = len - 3; i > 0; i -= 3) {
srcleft =
srcleft.substring(0, i)
+ COM + srcleft.substring(i);
}
if (negativeOk) {
srcleft = "- " + srcleft;
}
if (syousuu) {
src = srcleft + srcright;
} else {
src = srcleft;
}
}
return src;
}

------解决方案--------------------
这么多种。自己选择一种。物
String str = "3123726500.00 ";
NumberFormat currencyFormatter = DecimalFormat.getCurrencyInstance(Locale.CHINA);
double currencyNumber = Double.parseDouble(str);
System.out.println(currencyFormatter.format(new Double(currencyNumber)));
这个方法, 不错哦。
我喜欢 的是这种哦。
------解决方案--------------------
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class Test{
public static void main(String[] args) {
String str = "3123726500.00 ";
NumberFormat currencyFormatter = DecimalFormat
.getCurrencyInstance(Locale.CHINA);
double currencyNumber = Double.parseDouble(str);
System.out
.println(currencyFormatter.format(new Double(currencyNumber)));
}
}
可以的到运行结果:¥3,123,726,500.00

------解决方案--------------------
参照malligator(不能再整天泡在CSDN里了!) ( )
String str = "-26500.3443424 ";
String stra[] = str.split( ". ");
NumberFormat noFormatter = DecimalFormat.getNumberInstance();
double noNumber = Double.parseDouble(stra[0]);
String result = noFormatter.format(new Double(noNumber)) + stra[1];
System.out.println(result);
这样才是正解