Spell Out Number As Text

Wed, Mar 20

This tips shows how to output a numeric value spelled out as text. As an example, when writing a check you enter a numeric value: $99 as well as the spelled out variation of the same: ninety nine.

Let’s look at two examples:

// Number to output
NSNumber *value = [NSNumber numberWithInt:18099306];
 
// Create formatter
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
 
// Set formatter style to spell out
[formatter setNumberStyle: NSNumberFormatterSpellOutStyle];
 
NSLog (@"Numeric: %@\nText: %@", value, [formatter stringFromNumber:value]);

The output will look as follows:

Numeric: 18099306
Text: eighteen million ninety-nine thousand three hundred six

Here is another example with a number that includes a decimal point::

// Number to output
NSDecimalNumber *value = [NSDecimalNumber decimalNumberWithMantissa:106299 exponent:-2 isNegative:NO];
 
// Create formatter
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
 
// Set formatter style to spell out
[formatter setNumberStyle: NSNumberFormatterSpellOutStyle];
 
NSLog (@"Numeric: %@\nText: %@", value, [formatter stringFromNumber:value]);

This will return the following:

Numeric: 1062.99
Text: one thousand sixty-two point nine nine

One comment

Nice tip, thank you.

by tomowz on Apr 7, 2013. Reply #

Leave a Comment