1

我正在处理BigDecimalJava,我需要对BigDecimal我的 DTO 中的字段进行 2 次检查:

  1. 完整部分的位数(点前)< 15
  2. 总位数 < 32,包括刻度(点后为零)

实施它的最佳方法是什么?我非常不想toBigInteger().toString().toString()

4

1 回答 1

1

我认为这会奏效。

      BigDecimal d = new BigDecimal("921229392299229.2922929292920000");
      int fractionCount = d.scale();
      System.out.println(fractionCount);
      int wholeCount = (int) (Math.ceil(Math.log10(d.longValue())));
      System.out.println(wholeCount);

我对上述方法与使用indexOf和减法进行了一些测试lengths of strings如果我的测试方法合理,上述方法似乎要快得多。这是我测试它的方法。

      Random r = new Random(29);
      int nRuns = 1_000_000;
      // create a list of 1 million BigDecimals 

      List<BigDecimal> testData = new ArrayList<>();
      for (int j = 0; j < nRuns; j++) {

         String wholePart = r.ints(r.nextInt(15) + 1, 0, 10).mapToObj(
               String::valueOf).collect(Collectors.joining());

         String fractionalPart = r.ints(r.nextInt(31) + 1, 0, 10).mapToObj(
               String::valueOf).collect(Collectors.joining());

         BigDecimal d = new BigDecimal(wholePart + "." + fractionalPart);
         testData.add(d);
      }

      long start = System.nanoTime();
      // Using math
      for (BigDecimal d : testData) {
         int fractionCount = d.scale();
         int wholeCount = (int) (Math.ceil(Math.log10(d.longValue())));
      }

      long time = System.nanoTime() - start;
      System.out.println(time / 1_000_000.);

      start = System.nanoTime();
      //Using strings
      for (BigDecimal d : testData) {
         String sd = d.toPlainString();
         int n = sd.indexOf(".");
         int m = sd.length() - n - 1;
      }

      time = System.nanoTime() - start;
      System.out.println(time / 1_000_000.);
   }
于 2019-10-01T17:26:01.157 回答