# How to use Foundation's new FormatStyle to output spell out number?

**URL:** <https://forums.swift.org/t/how-to-use-foundations-new-formatstyle-to-output-spell-out-number/54552>\
**Category:** Using Swift\
**Created:** [January 10, 2022, 10:17pm UTC](https://forums.swift.org/t/how-to-use-foundations-new-formatstyle-to-output-spell-out-number/54552 "2022-01-10T22:17:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [January 10, 2022, 10:17pm UTC](https://forums.swift.org/t/how-to-use-foundations-new-formatstyle-to-output-spell-out-number/54552/1 "2022-01-10T22:17:04Z")

</div>

The old `NumberFormatter` let you output in [`.spellOut`](https://developer.apple.com/documentation/foundation/numberformatter/style) style.

But I cannot find this same functionality for the new [`FormatStyle`](https://developer.apple.com/documentation/foundation/formatstyle). I only see `.number` and `.percent`.

Can the new `FormatStyle` do `.spellOut` format?

Edit: Seems spell out formatting it's not supported. So I create it:

```swift
import SwiftUI

protocol NSNumberConvertible {
    /// Create an NSNumber out of this concrete numeric type
    var nsNumber: NSNumber { get }
}

extension Decimal: NSNumberConvertible {
    var nsNumber: NSNumber { self as NSNumber }
}
extension Double: NSNumberConvertible {
    var nsNumber: NSNumber { NSNumber(value: self) }
}
extension Float: NSNumberConvertible {
    var nsNumber: NSNumber { NSNumber(value: self) }
}
extension Int8: NSNumberConvertible {
    var nsNumber: NSNumber { NSNumber(value: self) }
}
extension Int32: NSNumberConvertible {
    var nsNumber: NSNumber { NSNumber(value: self) }
}
extension Int: NSNumberConvertible {
    var nsNumber: NSNumber { NSNumber(value: self) }
}
extension Int64: NSNumberConvertible {
    var nsNumber: NSNumber { NSNumber(value: self) }
}
extension Int16: NSNumberConvertible {
    var nsNumber: NSNumber { NSNumber(value: self) }
}

// all the unsigned type we support
extension UInt8: NSNumberConvertible {
    var nsNumber: NSNumber { NSNumber(value: self) }
}
extension UInt32: NSNumberConvertible {
    var nsNumber: NSNumber { NSNumber(value: self) }
}
extension UInt: NSNumberConvertible {
    var nsNumber: NSNumber { NSNumber(value: self) }
}
extension UInt64: NSNumberConvertible {
    var nsNumber: NSNumber { NSNumber(value: self) }
}
extension UInt16: NSNumberConvertible {
    var nsNumber: NSNumber { NSNumber(value: self) }
}

// Prefab one, is there better way. Can't put this in SpelloutNumberFormatStyle with static computed property
let spelloutNumberFormatter: NumberFormatter = {
    let formatter = NumberFormatter()
    formatter.numberStyle = .spellOut
    return formatter
}()

struct SpelloutNumberFormatStyle<Value: NSNumberConvertible>: FormatStyle {
    func format(_ value: Value) -> String {
        spelloutNumberFormatter.string(from: value.nsNumber)!
    }
}

extension FormatStyle where Self == SpelloutNumberFormatStyle<Decimal> {
    static var spellOut: Self { .init() }
}
extension FormatStyle where Self == SpelloutNumberFormatStyle<Double> {
    static var spellOut: Self { .init() }
}
extension FormatStyle where Self == SpelloutNumberFormatStyle<Float> {
    static var spellOut: Self { .init() }
}
extension FormatStyle where Self == SpelloutNumberFormatStyle<Int8> {
    static var spellOut: Self { .init() }
}
extension FormatStyle where Self == SpelloutNumberFormatStyle<Int32> {
    static var spellOut: Self { .init() }
}
extension FormatStyle where Self == SpelloutNumberFormatStyle<Int> {
    static var spellOut: Self { .init() }
}
extension FormatStyle where Self == SpelloutNumberFormatStyle<Int64> {
    static var spellOut: Self { .init() }
}
extension FormatStyle where Self == SpelloutNumberFormatStyle<Int16> {
    static var spellOut: Self { .init() }
}
extension FormatStyle where Self == SpelloutNumberFormatStyle<UInt8> {
    static var spellOut: Self { .init() }
}
extension FormatStyle where Self == SpelloutNumberFormatStyle<UInt> {
    static var spellOut: Self { .init() }
}
extension FormatStyle where Self == SpelloutNumberFormatStyle<UInt64> {
    static var spellOut: Self { .init() }
}
extension FormatStyle where Self == SpelloutNumberFormatStyle<UInt16> {
    static var spellOut: Self { .init() }
}

```

Did I do it correctly? Any better way?

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [January 11, 2022, 11:33am UTC](https://forums.swift.org/t/how-to-use-foundations-new-formatstyle-to-output-spell-out-number/54552/2 "2022-01-11T11:33:36Z")

</div>

> [@young](#):
>
> Can the new `FormatStyle` do `.spellOut` format?

Maybe it's not implemented yet? I can see a relevantly named `DescriptiveNumberFormatConfiguration.Presentation.spellOut` but don't see any public API that it can be used with.

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [January 11, 2022, 5:38pm UTC](https://forums.swift.org/t/how-to-use-foundations-new-formatstyle-to-output-spell-out-number/54552/3 "2022-01-11T17:38:22Z")

</div>

I traverse [back up a couple of levels](https://developer.apple.com/documentation/foundation/data_formatting/format_style_configurations?changes=_9) and look around. I don't know what `DescriptiveNumberFormatConfiguration` are used for. Wonder what these things are?

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [March 7, 2022, 5:05pm UTC](https://forums.swift.org/t/how-to-use-foundations-new-formatstyle-to-output-spell-out-number/54552/4 "2022-03-07T17:05:32Z")

</div>

`DescriptiveNumberFormatConfiguration.Presentation.spellOut` really not used anywhere as of now? Can any "insider" confirm?

---

<div class="post-metadata">

**Author:** ![eskimo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eskimo/32/13558_2.png) [@eskimo](https://forums.swift.org/u/eskimo)\
**Post date:** [March 8, 2022, 9:04am UTC](https://forums.swift.org/t/how-to-use-foundations-new-formatstyle-to-output-spell-out-number/54552/5 "2022-03-08T09:04:43Z")

</div>

> `DescriptiveNumberFormatConfiguration.Presentation.spellOut` really  
> not used anywhere as of now?

It’s used internally by the byte count formatter:

```swift
var fs = ByteCountFormatStyle.byteCount(style: .file)
let s0a = "Downloaded: \(0.formatted(fs))"
print(s0a)
// Downloaded: Zero kB
fs.spellsOutZero = false
let s0b = "Downloaded: \(0.formatted(fs))"
print(s0b)
// Downloaded: 0 bytes

```

I don’t know if there’s any other way to invoke it.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple
