获取BigDecimal数字的千分位表示,同时保留精度

获取BigDecimal数字的千分位表示,同时保留精度

需求:BigDecimal ----> String

两种方法:

  1. 自己编写逻辑方法
  2. 使用 java 提供的 API 来转化
/**
 * 转化一:自己拼接
 */
private static void formatMoney2(BigDecimal money) {
  DecimalFormat decimalFormat = new DecimalFormat("#,###");
  String[] parts = money.toString().split("\\.");
  String res = "";
  if (parts.length == 2) {
    res = decimalFormat.format(money.longValue()) + "." + parts[1];
  } else {
    res = decimalFormat.format(money.longValue());
  }
  System.out.printf("%d %50s\n", money.scale(), res);
}

/**
 * 转化二:使用 API, java.text.NumberFormat
 */
static void formatMoney(BigDecimal money) {
  NumberFormat instance = NumberFormat.getInstance();
  instance.setMinimumFractionDigits(money.scale());
  System.out.printf("%d %50s\n", money.scale(), instance.format(money));
}

/**
 * 测试
 */
@Test
public void t3() {
  Integer fl = 0;//保留小数位数
  for (; fl < 30; fl++) {
    formatMoney(gainBigDecimal(new BigDecimal("0.456"), fl));//注:如果传入数字 0.456,则有精度问题!
    formatMoney(gainBigDecimal(new BigDecimal("12.456"), fl));
    formatMoney(gainBigDecimal(new BigDecimal("123.456"), fl));
    formatMoney(gainBigDecimal(new BigDecimal("1234.456"), fl));
    formatMoney(gainBigDecimal(new BigDecimal("1234567890.456"), fl));
    System.out.println("-------");
  }
}

/**
 * 创建数值,设置保留小数位数
 */
public BigDecimal gainBigDecimal(BigDecimal value, Integer scale) {
  if (scale != null) {
    value = value.setScale(scale, RoundingMode.HALF_UP);
  }
  return value;
}

测试

0                                                  0
0                                                 12
0                                                123
0                                              1,234
0                                      1,234,567,890
-------
1                                                0.5
1                                               12.5
1                                              123.5
1                                            1,234.5
1                                    1,234,567,890.5
-------
2                                               0.46
2                                              12.46
2                                             123.46
2                                           1,234.46
2                                   1,234,567,890.46
-------
3                                              0.456
3                                             12.456
3                                            123.456
3                                          1,234.456
3                                  1,234,567,890.456
-------
4                                             0.4560
4                                            12.4560
4                                           123.4560
4                                         1,234.4560
4                                 1,234,567,890.4560
-------
5                                            0.45600
5                                           12.45600
5                                          123.45600
5                                        1,234.45600
5                                1,234,567,890.45600
-------
6                                           0.456000
6                                          12.456000
6                                         123.456000
6                                       1,234.456000
6                               1,234,567,890.456000
-------
7                                          0.4560000
7                                         12.4560000
7                                        123.4560000
7                                      1,234.4560000
7                              1,234,567,890.4560000
-------
8                                         0.45600000
8                                        12.45600000
8                                       123.45600000
8                                     1,234.45600000
8                             1,234,567,890.45600000
-------
9                                        0.456000000
9                                       12.456000000
9                                      123.456000000
9                                    1,234.456000000
9                            1,234,567,890.456000000
-------
10                                       0.4560000000
10                                      12.4560000000
10                                     123.4560000000
10                                   1,234.4560000000
10                           1,234,567,890.4560000000
-------
11                                      0.45600000000
11                                     12.45600000000
11                                    123.45600000000
11                                  1,234.45600000000
11                          1,234,567,890.45600000000
-------
12                                     0.456000000000
12                                    12.456000000000
12                                   123.456000000000
12                                 1,234.456000000000
12                         1,234,567,890.456000000000
-------
13                                    0.4560000000000
13                                   12.4560000000000
13                                  123.4560000000000
13                                1,234.4560000000000
13                        1,234,567,890.4560000000000
-------
14                                   0.45600000000000
14                                  12.45600000000000
14                                 123.45600000000000
14                               1,234.45600000000000
14                       1,234,567,890.45600000000000
-------
15                                  0.456000000000000
15                                 12.456000000000000
15                                123.456000000000000
15                              1,234.456000000000000
15                      1,234,567,890.456000000000000
-------
16                                 0.4560000000000000
16                                12.4560000000000000
16                               123.4560000000000000
16                             1,234.4560000000000000
16                     1,234,567,890.4560000000000000
-------
17                                0.45600000000000000
17                               12.45600000000000000
17                              123.45600000000000000
17                            1,234.45600000000000000
17                    1,234,567,890.45600000000000000
-------
18                               0.456000000000000000
18                              12.456000000000000000
18                             123.456000000000000000
18                           1,234.456000000000000000
18                   1,234,567,890.456000000000000000
-------
19                              0.4560000000000000000
19                             12.4560000000000000000
19                            123.4560000000000000000
19                          1,234.4560000000000000000
19                  1,234,567,890.4560000000000000000
-------
20                             0.45600000000000000000
20                            12.45600000000000000000
20                           123.45600000000000000000
20                         1,234.45600000000000000000
20                 1,234,567,890.45600000000000000000
-------
21                            0.456000000000000000000
21                           12.456000000000000000000
21                          123.456000000000000000000
21                        1,234.456000000000000000000
21                1,234,567,890.456000000000000000000
-------
22                           0.4560000000000000000000
22                          12.4560000000000000000000
22                         123.4560000000000000000000
22                       1,234.4560000000000000000000
22               1,234,567,890.4560000000000000000000
-------
23                          0.45600000000000000000000
23                         12.45600000000000000000000
23                        123.45600000000000000000000
23                      1,234.45600000000000000000000
23              1,234,567,890.45600000000000000000000
-------
24                         0.456000000000000000000000
24                        12.456000000000000000000000
24                       123.456000000000000000000000
24                     1,234.456000000000000000000000
24             1,234,567,890.456000000000000000000000
-------
25                        0.4560000000000000000000000
25                       12.4560000000000000000000000
25                      123.4560000000000000000000000
25                    1,234.4560000000000000000000000
25            1,234,567,890.4560000000000000000000000
-------
26                       0.45600000000000000000000000
26                      12.45600000000000000000000000
26                     123.45600000000000000000000000
26                   1,234.45600000000000000000000000
26           1,234,567,890.45600000000000000000000000
-------
27                      0.456000000000000000000000000
27                     12.456000000000000000000000000
27                    123.456000000000000000000000000
27                  1,234.456000000000000000000000000
27          1,234,567,890.456000000000000000000000000
-------
28                     0.4560000000000000000000000000
28                    12.4560000000000000000000000000
28                   123.4560000000000000000000000000
28                 1,234.4560000000000000000000000000
28         1,234,567,890.4560000000000000000000000000
-------
29                    0.45600000000000000000000000000
29                   12.45600000000000000000000000000
29                  123.45600000000000000000000000000
29                1,234.45600000000000000000000000000
29        1,234,567,890.45600000000000000000000000000
-------

我爱 cnblogs,爱它的界面清爽!定制化!广告少!

posted @ 2023-02-02 19:06  egu0o  阅读(1010)  评论(0编辑  收藏  举报