36

Is there a way in Objective-C on iOS to spell out an integer number as text?

For example, if I have

NSInteger someNumber = 11242043;

I would like to know some function so that would return a string similar to "eleven million two hundred forty two thousand forty three."

Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
cabgef
  • 1,398
  • 3
  • 19
  • 35

7 Answers7

63

Apple has a lot of handy formatting functionality built in for many data types. Called a "formatter," they can convert objects to/from string representations.

For your case, you will be using NSNumberFormatter, but if you have an integer you need to convert it to an NSNumber first. See below example.

NSInteger anInt = 11242043;
NSString *wordNumber;

//convert to words
NSNumber *numberValue = [NSNumber numberWithInt:anInt]; //needs to be NSNumber!
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
wordNumber = [numberFormatter stringFromNumber:numberValue];
NSLog(@"Answer: %@", wordNumber);
// Answer: eleven million two hundred forty-two thousand forty-three

If you'd like to learn more about formatters: https://developer.apple.com/library/content/documentation/General/Conceptual/Devpedia-CocoaApp/Formatter.html

Cœur
  • 37,241
  • 25
  • 195
  • 267
moca
  • 656
  • 5
  • 3
  • Thanks! Worked like a charm! – cabgef Feb 14 '12 at 15:50
  • `@(anInt)` would have been a slightly more compact way to write it; based on a quick test the NSNumberFormatter also isn't perfect at localisation (despite being in British English, I got hyphenated versions of forty and no 'and'), but it's probably still a million times better than building your own. – Tommy Jan 21 '16 at 13:21
37

Power of extension for Swift 5

import Foundation

public extension Int {
  var asWord: String? {
    let numberValue = NSNumber(value: self)
    let formatter = NumberFormatter()
    formatter.numberStyle = .spellOut
    return formatter.string(from: numberValue)
  }
}

var value = 2
if let valueAsWord = value.asWord {
    //do something with your worded number here
    print("value worded = \(valueAsWord)")
} else {
    print("could not word value :(")
}

Note: Edited to protect against formatter.string(from: returning nil which is highly not likely, but still possible.

Output: value worded = two

uplearned.com
  • 3,393
  • 5
  • 44
  • 59
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
10

From the docs:

NSNumberFormatterSpellOutStyle

Specifies a spell-out format; for example, “23” becomes “twenty-three”.

Available in iOS 2.0 and later.

Declared in NSNumberFormatter.h.

As your question isn't very specific, I won't post full-fledged code source either.

Community
  • 1
  • 1
Cyrille
  • 25,014
  • 12
  • 67
  • 90
8

With Swift 5 / iOS 12.2, NumberFormatter has a numberStyle property that can be set with value NumberFormatter.Style.spellOut. spellOut has the following declaration:

case spellOut = 5

A style format in which numbers are spelled out in the language defined by the number formatter locale.

For example, in the en_US locale, the number 1234.5678 is represented as one thousand two hundred thirty-four point five six seven eight; in the fr_FR locale, the number 1234.5678 is represented as mille deux cent trente-quatre virgule cinq six sept huit.

This style is supported for most user locales. If this style doesn't support the number formatter locale, the en_US locale is used as a fallback.


The Playground code below shows how to convert an integer to a spell-out text using NumberFormatter spellOut style:

import Foundation

let integer = 2018

let formatter = NumberFormatter()
formatter.numberStyle = NumberFormatter.Style.spellOut

let spellOutText = formatter.string(for: integer)!
print(spellOutText) // prints: two thousand eighteen
Imanou Petit
  • 89,880
  • 29
  • 256
  • 218
6

We can do this in swift like this.

   let formatter = NSNumberFormatter()
   formatter.numberStyle = NSNumberFormatterStyle. SpellOutStyle
   println("\(identifier) \(formatter.stringFromNumber(1234.5678))")
Govind
  • 2,337
  • 33
  • 43
5

You can use the below function to convert an integer to words using swift native number style.

func toWords<N>(number: N) -> String? {
    let formatter = NumberFormatter()
    formatter.numberStyle = .spellOut

    switch number {
    case is Int, is UInt, is Float, is Double:
        return formatter.string(from: number as! NSNumber)
    case is String:
        if let number = Double(number as! String) {
            return formatter.string(from: NSNumber(floatLiteral: number))
        }
    default:
        break
    }
    return nil
}

print(toWords(number: 12312))

print(toWords(number: "12312"))
Parth Adroja
  • 13,198
  • 5
  • 37
  • 71
1

For my own reference, this is @moca's answer, but ready for use:

- (NSString *) spellInt:(int)number {
    NSNumber *numberAsNumber = [NSNumber numberWithInt:number];
    NSNumberFormatter *formatter = [NSNumberFormatter new];
    [formatter setNumberStyle:NSNumberFormatterSpellOutStyle];
    return [formatter stringFromNumber:numberAsNumber];
}

Note: This is using ARC.

Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421