24 lines
865 B
Swift
24 lines
865 B
Swift
import Foundation
|
|
|
|
enum CurrencyFormatter {
|
|
static func currentCurrencyCode() -> String {
|
|
let context = CoreDataStack.shared.viewContext
|
|
return AppSettings.getOrCreate(in: context).currency
|
|
}
|
|
|
|
static func format(_ decimal: Decimal, style: NumberFormatter.Style = .currency, maximumFractionDigits: Int = 2) -> String {
|
|
let formatter = NumberFormatter()
|
|
formatter.numberStyle = style
|
|
formatter.currencyCode = currentCurrencyCode()
|
|
formatter.maximumFractionDigits = maximumFractionDigits
|
|
return formatter.string(from: decimal as NSDecimalNumber) ?? "\(decimal)"
|
|
}
|
|
|
|
static func symbol(for code: String) -> String {
|
|
let formatter = NumberFormatter()
|
|
formatter.numberStyle = .currency
|
|
formatter.currencyCode = code
|
|
return formatter.currencySymbol ?? code
|
|
}
|
|
}
|