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

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

问题描述

如何在Java中设置千位分隔符?
我有一个 BigDecimal 的字符串表示,我想用千位分隔符格式化并作为字符串返回.

How to set thousands separator in Java?
I have String representation of a BigDecimal that I want to format with a thousands separator and return as 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天全站免登陆