如何在Java中设置千位分隔符? [英] How to set thousands separator in Java?

查看:1324
本文介绍了如何在Java中设置千位分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java中设置千位分隔符?
我有BigDecimal的String表示,我想设置千位分隔符并返回String。

How to set thousands separator in Java? I have String representation of BigDecimal, i want to set thousands separator and return String.

推荐答案

这应该有用(未经测试,基于JavaDoc):

This should work (untested, based on JavaDoc):

DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();

symbols.setGroupingSeparator(' ');
formatter.setDecimalFormatSymbols(symbols);
System.out.println(formatter.format(bd.longValue()));

根据JavaDoc,第一行中的强制转换应该保存为大多数语言环境。

According to the JavaDoc, the cast in the first line should be save for most locales.

这篇关于如何在Java中设置千位分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆