Words for integers in Swift 5

I may have been dreaming, but I have a distinct memory of an Int property that does this:


let one = 1.property


where one would be set to the string "one". I have been googling for ages and can't find any reference to it. I've tried 1.description, but that just comes out as the string "1".

Replies

Define an extension with the property.


here is an example, with 2 properties:


extension Int {
    var doubleIt: Int {
        return self * 2
    }
   
    var asString: String {
        return String(self)
    }
}

print(1.doubleIt, 1.asString)


yields

2 1


You have an interesting example in Swift language book in Trailing Closures paragraph to

« use the map(_:) method with a trailing closure to convert an array of Int values into an array of String values. The array [16, 58, 510] is used to create the new array ["OneSix", "FiveEight", "FiveOneZero"]: »

Extrait de: Apple Inc. « The Swift Programming Language (Swift 4). » Apple Books.


So, inspired by this, you can write a spelled property:


extension Int {
    var doubleIt: Int {
        return self * 2
    }
   
    var asString: String {
        return String(self)
    }

    var spelled: String {
        let digitNames = [
            0: "Zero", 1: "One", 2: "Two",   3: "Three", 4: "Four",
            5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine"
        ]
        var number = self > 0 ? self : -self
        var output = ""
        repeat {
            output = digitNames[number % 10]! + (output == "" ? "": "-") + output
            number /= 10
        } while number > 0
        if self < 0 { output = "minus " + output }
        return output
    }
}


print(1.doubleIt)
print(1.asString)
print((-1).spelled)
print(25.spelled)

will yield

2

1

minus One

Two-Five


You can play with it and refine so that 25 yields twenty five

In fact, number formatter does it:

let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .spellOut

let string = numberFormatter.string(from: -25) ?? ""
print(string)


yields

minus twenty-five

As already shown, NumberFormatter with numberStyle .spellOut would be the one you want.


You can write an extension for Numeric which includes all integers.

let spellOutFormatter: NumberFormatter = {
    let nf = NumberFormatter()
    nf.numberStyle = .spellOut
    nf.locale = Locale(identifier: "en_US") //You may want to try some other locales...
    return nf
}()

extension Numeric {
    var spelledOut: String {
        return spellOutFormatter.string(for: self) ?? String(describing: self)
    }
}
let one = 1.spelledOut
print(one) //->one
print(25.spelledOut) //->twenty-five
print(1.5.spelledOut) //->one point five

Always fun to test

numberFormatter.locale = Locale(identifier: "ES-es")

menos veinticinco


numberFormatter.locale = Locale(identifier: "FR-fr")

moins vingt-cinq


numberFormatter.locale = Locale(identifier: "DE-de")

minus fünfundzwanzig


and hundreds others…

I may have been dreaming, but I have a distinct memory of an Int property that does this:

AFAIK such a property has never been built in. It’s possible you were dreaming, or you were using an extension from some third party, or you’re remembering something from some other platform. Regardless,

NumberFormatter
is your friend, as explained by Claude31 and OOPer.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"