大十进制值在除法时替换或得到正确的结果



我有两个类,其含义是取可用值(日期、金额和汇率(。接下来,填写日期之间的空白,以前的值

class ReportRow {
private LocalDate date;
private BigDecimal amount;
private BigDecimal rate;
public ReportRow(LocalDate date, BigDecimal amount, BigDecimal rate) {
this.date = date;
this.amount = amount;
this.rate = rate;
}
public LocalDate getDate() {
return date;
}
public BigDecimal getAmount() {
return amount;
}
public BigDecimal getRate() {
return rate;
}
@Override
public String toString() {
return date + " | " + amount + " | " + rate;
}
}
public class Main {
public static void main(String[] args) {
List<ReportRow> originalReport = List.of(
new ReportRow(LocalDate.of(2022, 6, 20), BigDecimal.valueOf(10000), BigDecimal.valueOf(3)),
new ReportRow(LocalDate.of(2023, 1, 15), BigDecimal.valueOf(8000), BigDecimal.valueOf(3)),
new ReportRow(LocalDate.of(2023, 7, 5), BigDecimal.valueOf(6500), BigDecimal.valueOf(3)));
System.out.println("Before:");
originalReport.forEach(System.out::println);
List<ReportRow> updatedReport = new ArrayList<>();
int size = originalReport.size();
updatedReport.add(originalReport.get(0));
for (int i = 1; i < size; i++) {
ReportRow lastRow = originalReport.get(i - 1);
ReportRow currentRow = originalReport.get(i);
BigDecimal rate = lastRow.getRate();

BigDecimal lastAmount = lastRow.getAmount();
LocalDate dateStart = lastRow.getDate();
LocalDate dateEnd = currentRow.getDate();
if (ChronoUnit.MONTHS.between(dateStart.withDayOfMonth(dateEnd.getDayOfMonth()), dateEnd) > 1) {
for (LocalDate date = dateStart.plusMonths(1); date.isBefore(dateEnd); date = date.plusMonths(1))
updatedReport.add(new ReportRow(date, lastAmount.divide(rate, RoundingMode.CEILING), rate));
}
updatedReport.add(currentRow);
}
System.out.println("After:");
updatedReport.forEach(System.out::println);

//for (ReportRow reportRow : originalReport) {
//    BigDecimal newAmount = reportRow.getAmount().divide(reportRow.getRate(), //RoundingMode.CEILING);
// System.out.println(newAmount);
}
}
}

结果,我得到了这样的结果:

Before:
2022-06-20 | 10000 | 3
2023-01-15 | 8000 | 3
2023-07-05 | 6500 | 3
After:
2022-06-20 | 10000 | 3
2022-07-20 | 3334 | 3
2022-08-20 | 3334 | 3
2022-09-20 | 3334 | 3
2022-10-20 | 3334 | 3
2022-11-20 | 3334 | 3
2022-12-20 | 3334 | 3
2023-01-15 | 8000 | 3
2023-02-15 | 2667 | 3
2023-03-15 | 2667 | 3
2023-04-15 | 2667 | 3
2023-05-15 | 2667 | 3
2023-06-15 | 2667 | 3
2023-07-05 | 6500 | 3

正如你从结果中看到的那样,我除以汇率的金额只除以需要填补的缺口,而主要金额保持不变。

我试图在输出中得到以下结果,这样所有金额都除以我的汇率:

Before:
2022-06-20 | 10000 | 3
2023-01-15 | 8000 | 3
2023-07-05 | 6500 | 3
After:
2022-06-20 | 3334 | 3
2022-07-20 | 3334 | 3
2022-08-20 | 3334 | 3
2022-09-20 | 3334 | 3
2022-10-20 | 3334 | 3
2022-11-20 | 3334 | 3
2022-12-20 | 3334 | 3
2023-01-15 | 2667 | 3
2023-02-15 | 2667 | 3
2023-03-15 | 2667 | 3
2023-04-15 | 2667 | 3
2023-05-15 | 2667 | 3
2023-06-15 | 2667 | 3
2023-07-05 | 2167 | 3

要获得所需的输出,Main类应该是:

public class Main {
public static void main(String[] args) {
List<ReportRow> originalReport = List.of(
new ReportRow(LocalDate.of(2022, 6, 20), BigDecimal.valueOf(10000), BigDecimal.valueOf(3)),
new ReportRow(LocalDate.of(2023, 1, 15), BigDecimal.valueOf(8000), BigDecimal.valueOf(3)),
new ReportRow(LocalDate.of(2023, 7, 5), BigDecimal.valueOf(6500), BigDecimal.valueOf(3)));
System.out.println("Before:");
originalReport.forEach(System.out::println);
List<ReportRow> updatedReport = new ArrayList<>();
int size = originalReport.size();
for (int i = 1; i < size; ++i) {
ReportRow lastRow = originalReport.get(i - 1);
ReportRow currentRow = originalReport.get(i);
BigDecimal rate = lastRow.getRate();
BigDecimal lastAmount = lastRow.getAmount();
LocalDate dateStart = lastRow.getDate();
LocalDate dateEnd = currentRow.getDate();
if (ChronoUnit.MONTHS.between(dateStart.withDayOfMonth(dateEnd.getDayOfMonth()), dateEnd) > 1) {
for (LocalDate date = dateStart; date.isBefore(dateEnd); date = date.plusMonths(1)) {
updatedReport.add(new ReportRow(date, lastAmount.divide(rate, RoundingMode.CEILING), rate));
}
}
if (i == (size - 1)) {
updatedReport.add(new ReportRow(dateEnd, currentRow.getAmount().divide(rate, RoundingMode.CEILING), rate));
}
}
System.out.println("After:");
updatedReport.forEach(System.out::println);
}
}

最新更新