Skip to content

Commit

Permalink
no trailing 0's on BigDecimal, more tests, removed redundant tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdereg committed Feb 9, 2025
1 parent 60da99e commit 2c78618
Show file tree
Hide file tree
Showing 8 changed files with 407 additions and 453 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.time.OffsetDateTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -72,12 +73,25 @@ static BigInteger toBigInteger(Object from, Converter converter) {
}

static BigDecimal toBigDecimal(Object from, Converter converter) {
// Cast to the expected type. (Consider changing the parameter type if possible.)
java.sql.Date sqlDate = (java.sql.Date) from;
return new BigDecimal(sqlDate.toLocalDate()
.atStartOfDay(converter.getOptions().getZoneId())
.toInstant()
.toEpochMilli())
.divide(BigDecimal.valueOf(1000), 9, RoundingMode.DOWN);

// Get the ZoneId from the converter options.
ZoneId zone = converter.getOptions().getZoneId();

// Convert the sqlDate to an Instant (at the start of day in the given zone).
Instant instant = sqlDate.toLocalDate().atStartOfDay(zone).toInstant();

// Convert the epoch millis into seconds.
// (We use a division with 9 digits of scale so that if there are fractional parts
// they are preserved, then we remove trailing zeros.)
BigDecimal seconds = BigDecimal.valueOf(instant.toEpochMilli())
.divide(BigDecimal.valueOf(1000), 9, RoundingMode.DOWN)
.stripTrailingZeros();

// Rebuild the BigDecimal from its plain string representation.
// This ensures that when you later call toString() it will not use exponential notation.
return new BigDecimal(seconds.toPlainString());
}

static Instant toInstant(Object from, Converter converter) {
Expand Down

Large diffs are not rendered by default.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 2c78618

Please sign in to comment.