Skip to content

feat(core): hideZeroFractionDigits option — hide decimals when zero#13

Merged
Merkost merged 4 commits into
mainfrom
feature/issue-5-hide-zero-fraction-digits
Jun 5, 2026
Merged

feat(core): hideZeroFractionDigits option — hide decimals when zero#13
Merkost merged 4 commits into
mainfrom
feature/issue-5-hide-zero-fraction-digits

Conversation

@Merkost

@Merkost Merkost commented Jun 5, 2026

Copy link
Copy Markdown
Member

Summary

Fixes #5. Adds an opt-in CurrencyFormatOptions.hideZeroFractionDigits flag: when the fractional part is all zeros it's dropped, while non-zero fractions keep full precision.

Input Currency Output
34.00 USD $34
34.20 USD $34.20
34.88 USD $34.88
val opts = CurrencyFormatOptions { hideZeroFractionDigits = true }
Kurrency.USD.formatAmountWithOptions("34.00", opts) // "$34"

Design

  • The flag is consumed in the common-code CurrencyFormatter.formatWithOptions engine: after the number is rounded and split, an all-zero fractional part is dropped (and so is the decimal separator).
  • Done after rounding (reflects displayed digits: 34.004 → $34) and after min/max resolution (so it wins over an explicit minFractionDigits).
  • No-op for zero-fraction currencies (JPY); grouping and negatives preserved.
  • The platform simple-path (formatAmount/CurrencyStyle) is intentionally not changed — it routes through the platform formatters, and adding the flag there would split one method across two engines.

Why not minFractionDigits?

Setting min=0 would turn 34.20 into 34.2 (strips the trailing zero). This flag is all-or-nothing: drop all fraction digits or keep the currency's full count.

kurrency-deci

Adds CurrencyFormatter.formatWithOptions(amount: Deci, …) overloads (currency code + Kurrency), closing the prior gap where Deci users had no options access — and giving an exact, decimal-native input.

Testing

Extensive commonTest coverage (runs on JVM/JS/WasmJs/iOS): all-zero dropped, non-zero kept, partial trailing zero, grouping preserved, round-down-to-zero, round-up-to-non-zero, zero amount, negatives, 3-digit currency (BHD), comma-decimal locale (German), JPY no-op, hide-wins-over-min, the high-level Kurrency.formatAmountWithOptions entry, serialization round-trip + absent-field default, and Deci (code + Kurrency + high-precision). All green on every platform.

Backward-compatible: new @Serializable field defaults to false.

Merkost and others added 3 commits June 5, 2026 13:09
Adds a boolean `hideZeroFractionDigits` field to `CurrencyFormatOptions`
(default `false`, backward-compatible). When `true`, the fractional part is
suppressed in `formatWithOptions` / `formatAmountWithOptions` if and only if
every digit in the rounded fraction is zero ("$34.00" → "$34"), while any
non-zero fraction keeps full precision ("$34.20" stays "$34.20"). The option
wins over `minFractionDigits`. Covered by 16 commonTest cases across USD,
BHD, JPY, EUR/German locale, negative amounts, grouping, and zero amounts.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Expose CurrencyFormatOptions to kurrency-deci by adding two extension
functions on CurrencyFormatter that accept a Deci amount and delegate
to the existing String-based formatWithOptions in core.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…nt from JSON

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 5, 2026 03:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces an opt-in CurrencyFormatOptions.hideZeroFractionDigits flag to suppress the decimal separator and fractional digits when the fractional portion is all zeros, while leaving non-zero fractions unchanged. It updates the common formatting engine to apply the behavior after precision resolution and rounding, adds Deci overloads for options-based formatting, and expands cross-platform test coverage including serialization.

Changes:

  • Add hideZeroFractionDigits to CurrencyFormatOptions (serializable, default false) and apply it during numeric reassembly in CurrencyFormatter.
  • Add CurrencyFormatter.formatWithOptions(amount: Deci, …) overloads in kurrency-deci.
  • Add common tests for hide/keep behavior, locale differences, rounding behavior, and serialization defaults/round-trip.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
kurrency-core/src/commonMain/kotlin/org/kimplify/kurrency/CurrencyFormatOptions.kt Adds hideZeroFractionDigits option to the serializable options model and builder.
kurrency-core/src/commonMain/kotlin/org/kimplify/kurrency/CurrencyFormatter.kt Drops the fractional part + decimal separator when the fraction is all zeros and the option is enabled.
kurrency-core/src/commonTest/kotlin/org/kimplify/kurrency/HideZeroFractionDigitsTest.kt Adds cross-locale/common tests covering hide/keep logic, rounding, negatives, and 0-fraction currencies.
kurrency-core/src/commonTest/kotlin/org/kimplify/kurrency/serialization/CurrencyFormatOptionsSerializerTest.kt Adds serialization round-trip and defaulting coverage for the new option.
kurrency-deci/src/commonMain/kotlin/org/kimplify/kurrency/deci/DeciExtensions.kt Adds Deci overloads for options-based formatting via CurrencyFormatter.
kurrency-deci/src/commonTest/kotlin/org/kimplify/kurrency/deci/DeciHideZeroFractionDigitsTest.kt Adds Deci-specific tests validating hide/keep and rounding-to-zero behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…eci) overloads

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Merkost Merkost merged commit dc1b2f4 into main Jun 5, 2026
2 checks passed
@Merkost Merkost deleted the feature/issue-5-hide-zero-fraction-digits branch June 5, 2026 03:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Option to not show decimal places if they are zero

2 participants