我从Json响应中获取一个字符串,如下所示:
"您的帐户余额为星期一和#34的100000;
在此我需要为数值添加逗号。任何人都可以帮助我如何添加这个。谢谢。
在这种情况下,我希望输出格式如下:
"您的帐户余额为星期一和#34的100,000;
答案 0 :(得分:3)
NumberFormat anotherFormat = NumberFormat.getNumberInstance(Locale.US);
if (anotherFormat instanceof DecimalFormat) {
DecimalFormat anotherDFormat = (DecimalFormat) anotherFormat;
anotherDFormat.applyPattern("#.00");
anotherDFormat.setGroupingUsed(true);
anotherDFormat.setGroupingSize(3);
for (double d : numbers) {
System.out.println(anotherDFormat.format(d));
}
}
答案 1 :(得分:0)
final String jsonString = "Your account balance is 100000 as on Monday";
DecimalFormat decFormat = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
decFormat.setGroupingUsed(true);
decFormat.setGroupingSize(3);
Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher(jsonString);
while (m.find()) {
System.out.println(decFormat.format(Double.valueOf(m.group())));
}