I wouldn't be sure that mixing their compact number notation with the
leading currency symbol
It’s worse than that. Different locales use different orders for their currency symbols, even for the same currency symbol. For example:
let n = 1234.56
var fs = FloatingPointFormatStyle<Double>.Currency(code: "EUR")
fs.locale = Locale(identifier: "en_IE")
print(n.formatted(fs))
// €1,234.56
fs.locale = Locale(identifier: "fr_FR")
print(n.formatted(fs))
// 1 234,56 €
Note that this output contains two different things that look like normal spaces but aren’t (-:
enobat, I think you might be able to make this work by using the currency formatter to render to an attributed string, using the attributes to find the numeric component of the output, and replacing that with the number from your number formatter. That’ll probably be wrong somewhere — one thing I’ve learnt about internationalise is that there are no universal truths — but I think it’ll work in most cases.
Here’s the first part of that process:
let n = 1234.56
var fs = FloatingPointFormatStyle<Double>.Currency(code: "EUR")
fs.locale = Locale(identifier: "en_IE")
let fsa = fs.attributed
let s = n.formatted(fsa)
print(String(s.characters))
// €1,234.56
guard
let start = s.runs[\.numberPart].first(where: { $0.0 == .integer})?.1.lowerBound,
let end = s.runs[\.numberPart].first(where: { $0.0 == .fraction})?.1.upperBound
else {
fatalError()
}
let numberRange = s[start..<end]
print(String(numberRange.characters))
// 1,234.56
Share and Enjoy
Quinn “The Eskimo!” @ DTS @ Apple