首页 > 其他分享 >获取BigDecimal数字的千分位表示,同时保留精度

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

时间:2023-02-02 19:24:46浏览次数:52  
标签:BigDecimal money 千分 567 ------- 234 fl 精度

获取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,爱它的界面清爽!定制化!广告少!

标签:BigDecimal,money,千分,567,-------,234,fl,精度
From: https://www.cnblogs.com/engure/p/17087169.html

相关文章

  • java的BigDecimal比较大小
    BigDecimala=newBigDecimal(10);BigDecimalb=newBigDecimal(2);if(a.compareTo(b)==0){System.out.println("a等于b");}if(a.compareTo(b)==1){......
  • ELK系列(4) - Elasticsearch cannot write xcontent for unknown value of type class
    问题与分析在使用Elasticsearch进行index数据时,发现报错如下:java.lang.IllegalArgumentException:cannotwritexcontentforunknownvalueoftypeclassjava.math.BigD......
  • 搞定物联网定位:UWB高精度定位技术原理与实现
    搞定物联网定位:UWB高精度定位技术原理与实现https://mp.weixin.qq.com/s/18kCIFtKCfSQGoZy4NDLaA搞定物联网定位:UWB高精度定位技术原理与实现原创 刘恒进 腾讯云开发......
  • Java百分比、BigDecimal小数互转
    1、百分比转为BigDecimal小数Stringpercent="66.60%";percent=percent.replace("%","");Floatf=Float.valueOf(percent)/100;BigDecimaldecimal=newBigDeci......
  • 高精度(有符号)
    #include<bits/stdc++.h>usingnamespacestd;usingll=longlong;structbig_base{ staticconstintL=1E4,MOD=10,B=1; //attention除法效率位数^2*MO......
  • PyTorch图像分类全流程实战--在测试集上评估图像分类算法精度05
    教程同济子豪兄https://space.bilibili.com/1900783https://www.bilibili.com/video/BV1qe4y1D7zD环境配置数据处理:numpypandas机器学习库:scikit-learn画图的:matpl......
  • API(BigInteger,BigDecimal)
    BigInteger的对象一旦创建,内部的值不会发生改变,如果参与运算,则会产生一个新的BigIneger对象来接收如果BigInteger的值没有超过long的范围,可以用静态方法获取//静态方法......
  • 高精度差分
    四、高精度:1.大整数的存储2.模拟加法的存储123+89=212(Ai+Bi+t)#include<vector>将数组的长度变长例题1.高精度减法#include<iostream>#include<vector>using......
  • 08_字符串扩展_4_格式化的精度控制
    """_*_coding:utf-8_*_@Time:2023/1/2221:01@Author:软柠柠吖@Description:格式化的精度控制我们可以使用辅助符号"m.n"来控制数据的宽度和......
  • 高精度
    当计算位数超过最大存储范围时,无法正常存储而使用数组来存储数据读取stringa,b;cin>>a>>b;//用字符串读取vector<int>A,B;//转换为存储在数组中for(inti......