Skip to content

Commit

Permalink
Add support for notation to currency format styles (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblukas committed Apr 2, 2024
1 parent 30fcfba commit 787b173
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,17 @@ extension Decimal.FormatStyle {
return new
}

/// Modifies the format style to use the specified notation.
///
/// - Parameter notation: The notation to apply to the format style.
/// - Returns: A decimal currency format style modified to use the specified notation.
@available(FoundationPreview 0.4, *)
public func notation(_ notation: Configuration.Notation) -> Self {
var new = self
new.collection.notation = notation
return new
}

// FormatStyle
public func format(_ value: Decimal) -> String {
if let f = ICUCurrencyNumberFormatter.create(for: self), let res = f.format(value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,17 @@ extension FloatingPointFormatStyle {
new.collection.presentation = p
return new
}

/// Modifies the format style to use the specified notation.
///
/// - Parameter notation: The notation to apply to the format style.
/// - Returns: A floating-point currency format style modified to use the specified notation.
@available(FoundationPreview 0.4, *)
public func notation(_ notation: Configuration.Notation) -> Self {
var new = self
new.collection.notation = notation
return new
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,17 @@ extension IntegerFormatStyle {
new.collection.presentation = p
return new
}

/// Modifies the format style to use the specified notation.
///
/// - Parameter notation: The notation to apply to the format style.
/// - Returns: An integer currency format style modified to use the specified notation.
@available(FoundationPreview 0.4, *)
public func notation(_ notation: Configuration.Notation) -> Self {
var new = self
new.collection.notation = notation
return new
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ public enum CurrencyFormatStyleConfiguration {
public typealias Precision = NumberFormatStyleConfiguration.Precision
public typealias DecimalSeparatorDisplayStrategy = NumberFormatStyleConfiguration.DecimalSeparatorDisplayStrategy
public typealias RoundingRule = NumberFormatStyleConfiguration.RoundingRule
/// The type used to configure notation for currency format styles.
@available(FoundationPreview 0.4, *)
public typealias Notation = NumberFormatStyleConfiguration.Notation

internal typealias RoundingIncrement = NumberFormatStyleConfiguration.RoundingIncrement
internal struct Collection : Codable, Hashable {
Expand All @@ -323,6 +326,7 @@ public enum CurrencyFormatStyleConfiguration {
var rounding: RoundingRule?
var roundingIncrement: RoundingIncrement?
var presentation: Presentation
var notation: Notation?
}

public struct SignDisplayStrategy: Codable, Hashable, Sendable {
Expand Down Expand Up @@ -842,6 +846,9 @@ extension CurrencyFormatStyleConfiguration.Collection {
if let rounding = rounding {
s += rounding.skeleton + " "
}
if let notation = notation {
s += notation.skeleton + " "
}

return s._trimmingWhitespace()
}
Expand Down

0 comments on commit 787b173

Please sign in to comment.