1

是否可以将两个整数相除以生成 BigDecimal?我正在尝试优化我的代码。如果我可以减少我声明新 BigDecimal 的次数,它应该会显着加快我的流程。到目前为止,我最了解的如何减少计算是:

BigDecimal tester = new BigDecimal("103993")
       .divide(new BigDecimal("33102"), 2000, BigDecimal.ROUND_DOWN);

我想知道是否可以做这样的事情:

int example1 = 103993;
int example2 = 33102;

BigDecimal example3 = example1/example2;

或类似的规定。我唯一的目标是加快执行时间。

4

2 回答 2

5

There is a slight improvement that you can do that uses the BigDecimal's internal cache by using its valueOf method:

BigDecimal.valueOf(example1).divide(
    BigDecimal.valueOf(example2), 2000, BigDecimal.ROUND_DOWN);
于 2013-03-02T17:29:27.140 回答
2

In a word, no. By the time you've divided one int by the other, you've already lost information.

于 2013-03-02T17:29:33.647 回答