Update formatting

This commit is contained in:
Ali
2021-04-02 21:19:54 +04:00
parent 20325dd69c
commit a6e2f05b3a
10 changed files with 1696 additions and 15 deletions

View File

@@ -46,6 +46,55 @@ private func loadCurrencyFormatterEntries() -> [String: CurrencyFormatterEntry]
private let currencyFormatterEntries = loadCurrencyFormatterEntries()
public func setupCurrencyNumberFormatter(currency: String) -> NumberFormatter {
guard let entry = currencyFormatterEntries[currency] ?? currencyFormatterEntries["USD"] else {
preconditionFailure()
}
var result = ""
if entry.symbolOnLeft {
result.append("¤")
if entry.spaceBetweenAmountAndSymbol {
result.append(" ")
}
}
result.append("#")
result.append(entry.decimalSeparator)
for _ in 0 ..< entry.decimalDigits {
result.append("#")
}
if entry.decimalDigits != 0 {
result.append("0")
}
if !entry.symbolOnLeft {
if entry.spaceBetweenAmountAndSymbol {
result.append(" ")
}
result.append("¤")
}
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currency
numberFormatter.positiveFormat = result
numberFormatter.negativeFormat = "-\(result)"
numberFormatter.currencySymbol = entry.symbol
numberFormatter.currencyDecimalSeparator = entry.decimalSeparator
numberFormatter.currencyGroupingSeparator = entry.thousandsSeparator
numberFormatter.minimumFractionDigits = entry.decimalDigits
numberFormatter.maximumFractionDigits = entry.decimalDigits
numberFormatter.minimumIntegerDigits = 1
return numberFormatter
}
public func fractionalToCurrencyAmount(value: Double, currency: String) -> Int64? {
guard let entry = currencyFormatterEntries[currency] ?? currencyFormatterEntries["USD"] else {
return nil
@@ -54,7 +103,11 @@ public func fractionalToCurrencyAmount(value: Double, currency: String) -> Int64
for _ in 0 ..< entry.decimalDigits {
factor *= 10.0
}
return Int64(value * factor)
if value > Double(Int64.max) / factor {
return nil
} else {
return Int64(value * factor)
}
}
public func currencyToFractionalAmount(value: Int64, currency: String) -> Double? {