如何分别格式化Double Value逗号

时间:2018-09-13 06:28:01

标签: java

我目前正在Spring MVC的工资支出模块中工作,在应用程序级别,我将工资数据存储为双精度类型eg double amount=32690597

我想将其格式设置为32,690,597,这样使其更具可读性。

建议我为此的最佳解决方案

3 个答案:

答案 0 :(得分:2)

使用DecimalFormat解决此问题。

double amount = 32690597;
//create pattern for decimal format
String pattern = "###,###";
DecimalFormat decimalFormat = new DecimalFormat(pattern);

//change dot with comma
String formattedNumber = decimalFormat.format(amount).replace(".",",");

详细的十进制格式化键;

0   A digit - always displayed, even if number has less digits (then 0 is displayed)
#   A digit, leading zeroes are omitted.
.   Marks decimal separator
,   Marks grouping separator (e.g. thousand separator)
E   Marks separation of mantissa and exponent for exponential formats.
;   Separates formats
-   Marks the negative number prefix
%   Multiplies by 100 and shows number as percentage
?   Multiplies by 1000 and shows number as per mille
¤   Currency sign - replaced by the currency sign for the Locale. Also makes formatting use the monetary decimal separator instead of normal decimal separator. ¤¤ makes formatting use international monetary symbols.
X   Marks a character to be used in number prefix or suffix
'   Marks a quote around special characters in prefix or suffix of formatted number.

一些例子;

Pattern      Number        Formatted String
###.###      123.456       123.456
###.#        123.456       123.5
###,###.##   123456.789    123,456.79
000.###      9.95          009.95
##0.###      0.95          0.95

答案 1 :(得分:1)

thing.read()

希望这会有所帮助

最好使用String.format

答案 2 :(得分:1)

System.out.println(String.format("%1$,.0f", amount));

OR

String formatAmount = new DecimalFormat("#,###,###,###,###").format(amount);

或-Java 8

NumberFormat numberFormatter = NumberFormat.getNumberInstance(Locale.ENGLISH);
String formatAmount = numberFormatter.format(amount);