Skip to content

Commit

Permalink
AdaptyUI: add unified price formatting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
x401om committed Aug 26, 2024
1 parent 979ae3d commit 275e93b
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions Sources/Extensions/Locale+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ import StoreKit

extension Locale: AdaptyExtended {}

extension NSDecimalNumber {
var isInteger: Bool {
return self == rounding(accordingToBehavior: nil)
}
}

public extension Locale {
func localized(price: NSDecimalNumber) -> String? {
ext.localized(price: price)
}
}

extension AdaptyExtension where Extended == Locale {
var currencyCode: String? {
guard #available(macOS 13, iOS 16, tvOS 16, watchOS 9, visionOS 1.0, *) else {
Expand All @@ -25,12 +37,16 @@ extension AdaptyExtension where Extended == Locale {
return this.region?.identifier
}

func localized(price: NSNumber) -> String? {
func localized(price: NSDecimalNumber) -> String? {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = this
formatter.minimumFractionDigits = 0
formatter.maximumFractionDigits = 2

if price.isInteger {
formatter.minimumFractionDigits = 0
formatter.maximumFractionDigits = 0
}

return formatter.string(from: price)
}

Expand Down

0 comments on commit 275e93b

Please sign in to comment.