mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-23 06:35:51 +00:00
Update formatting
This commit is contained in:
@@ -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? {
|
||||
|
||||
Reference in New Issue
Block a user