Extend all number types in Swift
We could create a protocol, extend it with a default implementation and make all our numeric types conform to it:
protocol FormattableNumeric {}
extension FormattableNumeric {
var localized: String {
guard let number = self as? NSNumber else { return "NaN" }
return number.description(withLocale: Locale.current)
}
}
extension Int: FormattableNumeric {}
extension UInt: FormattableNumeric {}
extension Float: FormattableNumeric {}
extension Double: FormattableNumeric {}
// etc.
Depending on the current locale, you can now get formatted numbers just so:
1000.localized // "1,000"
12_345_678.localized // "12,345,678"
(1_000_000 * Double.pi).localized // "3,141,592.65358979"
Of course, for more control over the formatting, we could also use NumberFormatter
in our implementation:
return NumberFormatter.localizedString(from: number, number: .decimal)
As Sogmeister
already said you will have to use Swift 2.0 to solve your problem.
Then you can do it like this:
// the solution right now is to implement it twice, I'll explain why
extension IntegerType {
func toLocalCurrency(fractDigits:Int = 2) -> String {
let formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
/* ... */
formatter.maximumFractionDigits = fractDigits
return formatter.stringFromNumber(self as! NSNumber)! // probably like this
}
func toLocalCurrencyWithoutFractionDigits() -> String {
return self.toLocalCurrency(0)
}
}
extension FloatingPointType {
// second implementation goes here
}
// some example
let someUInt = UInt(12340)
someUInt.toLocalCurrency() // returns "12.340,00 €" for me
Updated answer:
The basic idea is to extend MyProtocol
with default implementation of your functions and then extend IntegerType
and FloatingPointType
. But this won't happen in Swift 2.0 (see here). The reason why it's not working yet is here. Here is another solution, which is better then my first one.
protocol MyProtocol {}
extension MyProtocol {
func toLocalCurrency(fractDigits:Int = 2) -> String {
let formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
/* ... */
formatter.maximumFractionDigits = fractDigits
guard let newNumber = self as? NSNumber else { fatalError("this type is not convertable to NSNumber") }
return formatter.stringFromNumber(newNumber)!
}
func toLocalCurrencyWithoutFractionDigits() -> String {
return self.toLocalCurrency(0)
}
}
/* extend your number types you need */
extension Int : MyProtocol {}
extension Double : MyProtocol {} // done