feat(core): decimal-exact formatWithOptions + configurable RoundingMode#15
Merged
Conversation
Pure foundation for decimal-exact formatting: a public RoundingMode enum (HALF_EVEN, HALF_UP, DOWN, UP) and an internal Decimals object providing roundToScale, isZero, isNegative, abs, and isOne — all operating on decimal strings with no Double round-trip. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…noring roundingMode Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…al-exact Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR updates the formatWithOptions formatting engine in kurrency-core to be decimal-string based (eliminating the Double round-trip for the amount being formatted) and introduces a configurable RoundingMode on CurrencyFormatOptions, with corresponding cross-platform tests and kurrency-deci exactness coverage.
Changes:
- Added
RoundingMode(serializable) andCurrencyFormatOptions.roundingMode(defaultHALF_EVEN). - Implemented
internal object Decimalsfor string-based rounding and predicates, and routedCurrencyFormatter.formatWithOptions/formatDecimalthrough it. - Added/updated tests to cover exact formatting beyond
Doubleprecision and rounding-mode behavior (core + deci).
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| kurrency-deci/src/commonTest/kotlin/org/kimplify/kurrency/deci/DeciExactFormattingTest.kt | Adds a regression test proving Deci formatting remains exact beyond Double integer precision. |
| kurrency-deci/src/commonMain/kotlin/org/kimplify/kurrency/deci/DeciExtensions.kt | Removes outdated docs warning about Double-based rounding/precision. |
| kurrency-core/src/commonTest/kotlin/org/kimplify/kurrency/serialization/CurrencyFormatOptionsSerializerTest.kt | Adds serializer round-trip + defaulting tests for roundingMode. |
| kurrency-core/src/commonTest/kotlin/org/kimplify/kurrency/DecimalsTest.kt | Adds unit tests for string-based rounding and helpers. |
| kurrency-core/src/commonTest/kotlin/org/kimplify/kurrency/DecimalExactFormattingTest.kt | Adds integration tests validating exact formatting and rounding modes via formatWithOptions. |
| kurrency-core/src/commonMain/kotlin/org/kimplify/kurrency/RoundingMode.kt | Introduces the new rounding-mode enum used by options and formatting. |
| kurrency-core/src/commonMain/kotlin/org/kimplify/kurrency/Decimals.kt | Adds internal string-decimal rounding utilities used by the formatter. |
| kurrency-core/src/commonMain/kotlin/org/kimplify/kurrency/CurrencyFormatter.kt | Reworks formatWithOptions to be decimal-string based and plumbs roundingMode into rounding. |
| kurrency-core/src/commonMain/kotlin/org/kimplify/kurrency/CurrencyFormatOptions.kt | Adds roundingMode to options + builder, with docs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
isValidAmount accepts scientific notation (e.g. "1e3") via toDoubleOrNull, but the string-based formatWithOptions path would emit garbage like "1e3.00". Expand exponent notation to a plain decimal string first, matching the platform path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…onent cases Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #14. Makes
CurrencyFormatter.formatWithOptionsformat from the decimal string with noDoubleround-trip, and adds a configurable rounding mode toCurrencyFormatOptions.Why
The options path was
Double-based (toDoubleOrNull+round(value*factor)/factor), which lost precision beyondDouble's ~15–17 significant digits — defeating thekurrency-decioverloads — and inherited rounding artifacts ("2.675"rendered2.67because2.675*100is267.4999…).What changed
internal object Decimals— pure string decimal arithmetic (no dependency):roundToScale(s, scale, mode)with carry +isZero/isNegative/abs/isOne.RoundingMode(@Serializable):HALF_EVEN(default),HALF_UP,DOWN(truncate),UP.CurrencyFormatOptions.roundingMode— defaultHALF_EVEN, backward-compatible.CurrencyFormatter—formatDecimalstring-based; notoDoublein the value path; deadtenPowremoved.+/ locale-grouped input).Behavior change (intended)
Default stays half-even but is now mathematically exact, correcting
Doubleartifacts (2.675 → $2.68). No existing tests needed changing.Testing
DecimalsTest(18),DecimalExactFormattingTest(6), serializer round-trip + default (8), deci exactness (1) — allcommonTest, green on JVM/JS/WasmJs/iOS. Covers every mode, ties, carry, padding, scale 0, predicates, and beyond-Doublemagnitudes.kurrency-core.