-
Notifications
You must be signed in to change notification settings - Fork 1
feat(core): decimal-exact formatWithOptions + configurable RoundingMode #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2dd9a32
feat(core): add RoundingMode and exact decimal-string Decimals utility
Merkost f0e6911
feat(core): add roundingMode option to CurrencyFormatOptions
Merkost 9f03e1c
refactor(core): format options path with exact decimal arithmetic, ho…
Merkost ad1c22c
docs(deci): drop precision caveat now that formatWithOptions is decim…
Merkost a031581
fix(core): expand scientific notation before the exact decimal path
Merkost a16caf9
test(core): cover negative-with-rounding, default half-even, and +exp…
Merkost File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
66 changes: 66 additions & 0 deletions
66
kurrency-core/src/commonMain/kotlin/org/kimplify/kurrency/Decimals.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package org.kimplify.kurrency | ||
|
|
||
| internal object Decimals { | ||
|
|
||
| fun isZero(s: String): Boolean = s.none { it in '1'..'9' } | ||
|
|
||
| fun isNegative(s: String): Boolean = s.startsWith("-") && !isZero(s) | ||
|
|
||
| fun abs(s: String): String = if (s.startsWith("-") || s.startsWith("+")) s.substring(1) else s | ||
|
|
||
| fun isOne(s: String): Boolean { | ||
| val a = abs(s) | ||
| val dot = a.indexOf('.') | ||
| val intPart = (if (dot >= 0) a.substring(0, dot) else a).trimStart('0').ifEmpty { "0" } | ||
| val fracPart = if (dot >= 0) a.substring(dot + 1) else "" | ||
| return intPart == "1" && fracPart.all { it == '0' } | ||
| } | ||
|
|
||
| fun roundToScale(s: String, scale: Int, mode: RoundingMode): String { | ||
| val dot = s.indexOf('.') | ||
| val intDigits = (if (dot >= 0) s.substring(0, dot) else s).ifEmpty { "0" } | ||
| val fracPart = if (dot >= 0) s.substring(dot + 1) else "" | ||
|
|
||
| if (fracPart.length <= scale) { | ||
| val padded = fracPart.padEnd(scale, '0') | ||
| return if (scale == 0) intDigits else "$intDigits.$padded" | ||
| } | ||
|
|
||
| val kept = fracPart.substring(0, scale) | ||
| val dropped = fracPart.substring(scale) | ||
| val combined = intDigits + kept | ||
|
|
||
| val roundUp = when (mode) { | ||
| RoundingMode.DOWN -> false | ||
| RoundingMode.UP -> dropped.any { it != '0' } | ||
| RoundingMode.HALF_UP -> dropped[0] >= '5' | ||
| RoundingMode.HALF_EVEN -> when { | ||
| dropped[0] > '5' -> true | ||
| dropped[0] < '5' -> false | ||
| dropped.drop(1).any { it != '0' } -> true | ||
| else -> (combined.last() - '0') % 2 == 1 | ||
| } | ||
| } | ||
|
|
||
| val resultDigits = if (roundUp) incrementDigits(combined) else combined | ||
| val intResult = (if (scale == 0) resultDigits else resultDigits.dropLast(scale)) | ||
| .trimStart('0').ifEmpty { "0" } | ||
| val fracResult = if (scale == 0) "" else resultDigits.takeLast(scale) | ||
| return if (scale == 0) intResult else "$intResult.$fracResult" | ||
| } | ||
|
|
||
| private fun incrementDigits(digits: String): String { | ||
| val chars = digits.toCharArray() | ||
| var i = chars.size - 1 | ||
| while (i >= 0) { | ||
| if (chars[i] == '9') { | ||
| chars[i] = '0' | ||
| i-- | ||
| } else { | ||
| chars[i] = chars[i] + 1 | ||
| return chars.concatToString() | ||
| } | ||
| } | ||
| return "1" + chars.concatToString() | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
kurrency-core/src/commonMain/kotlin/org/kimplify/kurrency/RoundingMode.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package org.kimplify.kurrency | ||
|
|
||
| import kotlinx.serialization.Serializable | ||
|
|
||
| /** | ||
| * Rounding strategy applied when an amount has more fraction digits than the | ||
| * target scale. Used by [CurrencyFormatOptions.roundingMode]. | ||
| */ | ||
| @Serializable | ||
| enum class RoundingMode { | ||
| /** Round to the nearest neighbor; ties go to the even digit (banker's rounding). */ | ||
| HALF_EVEN, | ||
|
|
||
| /** Round to the nearest neighbor; ties round away from zero. */ | ||
| HALF_UP, | ||
|
|
||
| /** Round toward zero (truncate). */ | ||
| DOWN, | ||
|
|
||
| /** Round away from zero. */ | ||
| UP, | ||
| } |
43 changes: 43 additions & 0 deletions
43
kurrency-core/src/commonTest/kotlin/org/kimplify/kurrency/DecimalExactFormattingTest.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package org.kimplify.kurrency | ||
|
|
||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
|
|
||
| class DecimalExactFormattingTest { | ||
|
|
||
| private val us = CurrencyFormatter(KurrencyLocale.US) | ||
|
|
||
| @Test | ||
| fun exactBeyondDoublePrecision() { | ||
| val r = us.formatWithOptions("9007199254740993.01", "USD", CurrencyFormatOptions()).getOrThrow() | ||
| assertEquals("$9,007,199,254,740,993.01", r) | ||
| } | ||
|
|
||
| @Test | ||
| fun halfEvenDefault_correctsDoubleArtifact() { | ||
| assertEquals("$2.68", us.formatWithOptions("2.675", "USD", CurrencyFormatOptions()).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun roundingMode_halfUp() { | ||
| val opts = CurrencyFormatOptions { roundingMode = RoundingMode.HALF_UP } | ||
| assertEquals("$2.67", us.formatWithOptions("2.665", "USD", opts).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun roundingMode_down_truncates() { | ||
| val opts = CurrencyFormatOptions { roundingMode = RoundingMode.DOWN } | ||
| assertEquals("$2.67", us.formatWithOptions("2.679", "USD", opts).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun roundingMode_up_awayFromZero() { | ||
| val opts = CurrencyFormatOptions { roundingMode = RoundingMode.UP } | ||
| assertEquals("$2.68", us.formatWithOptions("2.671", "USD", opts).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun leadingPlus_isHandled() { | ||
| assertEquals("$5.00", us.formatWithOptions("+5", "USD", CurrencyFormatOptions()).getOrThrow()) | ||
| } | ||
| } |
102 changes: 102 additions & 0 deletions
102
kurrency-core/src/commonTest/kotlin/org/kimplify/kurrency/DecimalsTest.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package org.kimplify.kurrency | ||
|
|
||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertFalse | ||
| import kotlin.test.assertTrue | ||
|
|
||
| class DecimalsTest { | ||
|
|
||
| @Test fun halfEven_tieToEven_roundsDown() { | ||
| assertEquals("2.66", Decimals.roundToScale("2.665", 2, RoundingMode.HALF_EVEN)) | ||
| } | ||
|
|
||
| @Test fun halfEven_tieToEven_roundsUp() { | ||
| assertEquals("2.68", Decimals.roundToScale("2.675", 2, RoundingMode.HALF_EVEN)) | ||
| } | ||
|
|
||
| @Test fun halfEven_aboveHalf_roundsUp() { | ||
| assertEquals("2.68", Decimals.roundToScale("2.6751", 2, RoundingMode.HALF_EVEN)) | ||
| } | ||
|
|
||
| @Test fun halfEven_belowHalf_roundsDown() { | ||
| assertEquals("2.67", Decimals.roundToScale("2.6749", 2, RoundingMode.HALF_EVEN)) | ||
| } | ||
|
|
||
| @Test fun halfEven_scaleZero() { | ||
| assertEquals("2", Decimals.roundToScale("2.5", 0, RoundingMode.HALF_EVEN)) | ||
| assertEquals("4", Decimals.roundToScale("3.5", 0, RoundingMode.HALF_EVEN)) | ||
| } | ||
|
|
||
| @Test fun halfUp_tie_roundsAwayFromZero() { | ||
| assertEquals("2.67", Decimals.roundToScale("2.665", 2, RoundingMode.HALF_UP)) | ||
| assertEquals("3", Decimals.roundToScale("2.5", 0, RoundingMode.HALF_UP)) | ||
| } | ||
|
|
||
| @Test fun down_truncates() { | ||
| assertEquals("2.67", Decimals.roundToScale("2.679", 2, RoundingMode.DOWN)) | ||
| assertEquals("2", Decimals.roundToScale("2.999", 0, RoundingMode.DOWN)) | ||
| } | ||
|
|
||
| @Test fun up_anyRemainderRoundsUp() { | ||
| assertEquals("2.68", Decimals.roundToScale("2.671", 2, RoundingMode.UP)) | ||
| assertEquals("2.67", Decimals.roundToScale("2.670", 2, RoundingMode.UP)) | ||
| assertEquals("3", Decimals.roundToScale("2.001", 0, RoundingMode.UP)) | ||
| } | ||
|
|
||
| @Test fun carryPropagates() { | ||
| assertEquals("10.00", Decimals.roundToScale("9.999", 2, RoundingMode.HALF_UP)) | ||
| assertEquals("1", Decimals.roundToScale("0.999", 0, RoundingMode.HALF_UP)) | ||
| assertEquals("100.0", Decimals.roundToScale("99.95", 1, RoundingMode.HALF_UP)) | ||
| } | ||
|
|
||
| @Test fun padsWhenFewerDigitsThanScale() { | ||
| assertEquals("2.50", Decimals.roundToScale("2.5", 2, RoundingMode.HALF_EVEN)) | ||
| assertEquals("2.00", Decimals.roundToScale("2", 2, RoundingMode.HALF_EVEN)) | ||
| } | ||
|
|
||
| @Test fun beyondDoublePrecision_isExact() { | ||
| assertEquals( | ||
| "9007199254740993.01", | ||
| Decimals.roundToScale("9007199254740993.005", 2, RoundingMode.HALF_UP), | ||
| ) | ||
| } | ||
|
|
||
| @Test fun isZero_variants() { | ||
| assertTrue(Decimals.isZero("0")) | ||
| assertTrue(Decimals.isZero("0.00")) | ||
| assertTrue(Decimals.isZero("-0.0")) | ||
| assertFalse(Decimals.isZero("0.01")) | ||
| } | ||
|
|
||
| @Test fun isNegative_excludesNegativeZero() { | ||
| assertTrue(Decimals.isNegative("-3.50")) | ||
| assertFalse(Decimals.isNegative("-0.00")) | ||
| assertFalse(Decimals.isNegative("3.50")) | ||
| } | ||
|
|
||
| @Test fun abs_stripsSign() { | ||
| assertEquals("3.50", Decimals.abs("-3.50")) | ||
| assertEquals("3.50", Decimals.abs("3.50")) | ||
| } | ||
|
|
||
| @Test fun abs_stripsLeadingPlus() { | ||
| assertEquals("3.50", Decimals.abs("+3.50")) | ||
| } | ||
|
|
||
| @Test fun isOne_variants() { | ||
| assertTrue(Decimals.isOne("1")) | ||
| assertTrue(Decimals.isOne("1.00")) | ||
| assertTrue(Decimals.isOne("-1.0")) | ||
| assertFalse(Decimals.isOne("1.5")) | ||
| assertFalse(Decimals.isOne("10")) | ||
| } | ||
|
|
||
| @Test fun carryGrowsIntegerWidthAtNonZeroScale() { | ||
| assertEquals("100.00", Decimals.roundToScale("99.999", 2, RoundingMode.HALF_UP)) | ||
| } | ||
|
|
||
| @Test fun halfEven_tieWithTrailingNonZero_roundsUp() { | ||
| assertEquals("2.67", Decimals.roundToScale("2.6650001", 2, RoundingMode.HALF_EVEN)) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.