[ACCEPTED]-How do I convert an integer to the corresponding words in objective-c?-ios

Accepted answer
Score: 62

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

For 4 your case, you will be using NSNumberFormatter, but 3 if you have an integer you need to convert 2 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 1 you'd like to learn more about formatters: https://developer.apple.com/library/content/documentation/General/Conceptual/Devpedia-CocoaApp/Formatter.html

Score: 36

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 3 to protect against formatter.string(from: returning nil which 2 is highly not likely, but still possible.

Output: value 1 worded = two

Score: 10

From the docs:

NSNumberFormatterSpellOutStyle

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

Available 3 in iOS 2.0 and later.

Declared in NSNumberFormatter.h.

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

Score: 8

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

case spellOut = 5

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

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

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


The 2 Playground code below shows how to convert 1 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
Score: 6

We can do this in swift like this.

   let formatter = NSNumberFormatter()
   formatter.numberStyle = NSNumberFormatterStyle. SpellOutStyle
   println("\(identifier) \(formatter.stringFromNumber(1234.5678))")

0

Score: 5

You can use the below function to convert 2 an integer to words using swift native number 1 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"))
Score: 1

For my own reference, this is @moca's answer, but 1 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.

More Related questions