-
Notifications
You must be signed in to change notification settings - Fork 1
feat(core): hideZeroFractionDigits option — hide decimals when zero #13
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
822266e
feat(core): add hideZeroFractionDigits formatting option (issue #5)
Merkost 841dfde
feat(deci): add formatWithOptions(Deci) overloads
Merkost 4021cd7
test(core): assert hideZeroFractionDigits defaults to false when abse…
Merkost 1d08a03
docs(deci): note Double-based precision caveat on formatWithOptions(D…
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
128 changes: 128 additions & 0 deletions
128
kurrency-core/src/commonTest/kotlin/org/kimplify/kurrency/HideZeroFractionDigitsTest.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,128 @@ | ||
| package org.kimplify.kurrency | ||
|
|
||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertFalse | ||
| import kotlin.test.assertTrue | ||
|
|
||
| class HideZeroFractionDigitsTest { | ||
|
|
||
| private val us = CurrencyFormatter(KurrencyLocale.US) | ||
| private val de = CurrencyFormatter(KurrencyLocale.GERMANY) | ||
| private val hide = CurrencyFormatOptions { hideZeroFractionDigits = true } | ||
| private val show = CurrencyFormatOptions() // default: hideZeroFractionDigits = false | ||
|
|
||
| @Test | ||
| fun us_allZeroFraction_isDropped() { | ||
| assertEquals("$34", us.formatWithOptions("34.00", "USD", hide).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun us_nonZeroFraction_isKept() { | ||
| assertEquals("$34.20", us.formatWithOptions("34.20", "USD", hide).getOrThrow()) | ||
| assertEquals("$34.88", us.formatWithOptions("34.88", "USD", hide).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun us_partialTrailingZero_isKept() { | ||
| assertEquals("$34.50", us.formatWithOptions("34.50", "USD", hide).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun us_grouping_isPreservedWhenFractionDropped() { | ||
| assertEquals("$1,000", us.formatWithOptions("1000.00", "USD", hide).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun us_roundsDownToZero_isDropped() { | ||
| assertEquals("$34", us.formatWithOptions("34.004", "USD", hide).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun us_roundsToNonZeroFraction_isKept() { | ||
| assertEquals("$34.02", us.formatWithOptions("34.019", "USD", hide).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun us_nonZeroAmountRoundingToZero_isDropped() { | ||
| assertEquals("$0", us.formatWithOptions("0.004", "USD", hide).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun us_zeroAmount_isDropped() { | ||
| assertEquals("$0", us.formatWithOptions("0.00", "USD", hide).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun hideWinsOverMinFractionDigits() { | ||
| val opts = CurrencyFormatOptions { | ||
| hideZeroFractionDigits = true | ||
| minFractionDigits = 2 | ||
| } | ||
| assertEquals("$34", us.formatWithOptions("34.00", "USD", opts).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun disabledByDefault_keepsDecimals() { | ||
| assertEquals("$34.00", us.formatWithOptions("34.00", "USD", show).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun negative_allZeroFraction_isDropped() { | ||
| assertEquals("-$34", us.formatWithOptions("-34.00", "USD", hide).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun negative_nonZeroFraction_hideHasNoEffect() { | ||
| assertEquals( | ||
| us.formatWithOptions("-34.20", "USD", show).getOrThrow(), | ||
| us.formatWithOptions("-34.20", "USD", hide).getOrThrow(), | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun bhd_allZeroFraction_isDropped() { | ||
| val r = us.formatWithOptions("10.000", "BHD", hide).getOrThrow() | ||
| assertFalse(r.contains("."), "expected no decimals in: $r") | ||
| assertTrue(r.contains("10"), "expected amount in: $r") | ||
| } | ||
|
|
||
| @Test | ||
| fun bhd_nonZeroFraction_isKept() { | ||
| assertEquals( | ||
| us.formatWithOptions("10.500", "BHD", show).getOrThrow(), | ||
| us.formatWithOptions("10.500", "BHD", hide).getOrThrow(), | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun germanLocale_allZeroFraction_dropsCommaSeparator() { | ||
| val r = de.formatWithOptions("34.00", "EUR", hide).getOrThrow() | ||
| assertFalse(r.contains(","), "German decimal separator should be gone in: $r") | ||
| assertTrue(r.contains("34"), "expected amount in: $r") | ||
| } | ||
|
|
||
| @Test | ||
| fun germanLocale_nonZeroFraction_isKept() { | ||
| assertEquals( | ||
| de.formatWithOptions("34.20", "EUR", show).getOrThrow(), | ||
| de.formatWithOptions("34.20", "EUR", hide).getOrThrow(), | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun jpy_zeroDigitsCurrency_isNoOp() { | ||
| assertEquals( | ||
| us.formatWithOptions("34", "JPY", show).getOrThrow(), | ||
| us.formatWithOptions("34", "JPY", hide).getOrThrow(), | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun kurrencyFormatAmountWithOptions_propagatesOption() { | ||
| assertEquals( | ||
| "$34", | ||
| Kurrency.USD.formatAmountWithOptions("34.00", hide, KurrencyLocale.US).getOrThrow(), | ||
| ) | ||
| } | ||
| } |
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
42 changes: 42 additions & 0 deletions
42
...y-deci/src/commonTest/kotlin/org/kimplify/kurrency/deci/DeciHideZeroFractionDigitsTest.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,42 @@ | ||
| package org.kimplify.kurrency.deci | ||
|
|
||
| import org.kimplify.deci.Deci | ||
| import org.kimplify.kurrency.CurrencyFormatOptions | ||
| import org.kimplify.kurrency.CurrencyFormatter | ||
| import org.kimplify.kurrency.Kurrency | ||
| import org.kimplify.kurrency.KurrencyLocale | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
|
|
||
| class DeciHideZeroFractionDigitsTest { | ||
|
|
||
| private val formatter = CurrencyFormatter(KurrencyLocale.US) | ||
| private val hide = CurrencyFormatOptions { hideZeroFractionDigits = true } | ||
|
|
||
| @Test | ||
| fun deci_allZeroFraction_isDropped_withCurrencyCode() { | ||
| assertEquals("$34", formatter.formatWithOptions(Deci("34.00"), "USD", hide).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun deci_nonZeroFraction_isKept_withCurrencyCode() { | ||
| assertEquals("$34.20", formatter.formatWithOptions(Deci("34.20"), "USD", hide).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun deci_allZeroFraction_isDropped_withKurrency() { | ||
| val usd = Kurrency.fromCode("USD").getOrThrow() | ||
| assertEquals("$34", formatter.formatWithOptions(Deci("34.00"), usd, hide).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun deci_nonZeroFraction_isKept_withKurrency() { | ||
| val usd = Kurrency.fromCode("USD").getOrThrow() | ||
| assertEquals("$34.20", formatter.formatWithOptions(Deci("34.20"), usd, hide).getOrThrow()) | ||
| } | ||
|
|
||
| @Test | ||
| fun deci_highPrecisionFractionRoundsToZero_isDropped() { | ||
| assertEquals("$34", formatter.formatWithOptions(Deci("34.0000001"), "USD", hide).getOrThrow()) | ||
| } | ||
| } |
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.