Skip to content

Instantly share code, notes, and snippets.

@erkanyildiz
Created July 27, 2014 21:41
Show Gist options
  • Save erkanyildiz/a393ba48e25d3306030a to your computer and use it in GitHub Desktop.
Save erkanyildiz/a393ba48e25d3306030a to your computer and use it in GitHub Desktop.
NSNumber+TurkishSpellOut
// erkanyildiz
// 20140728-0035
//
// NSNumber+TurkishSpellOut.h
#import <Foundation/Foundation.h>
@interface NSNumber (TurkishSpellOut)
-(NSString*)spellOutTurkish;
@end
// erkanyildiz
// 20140728-0035
//
// NSNumber+TurkishSpellOut.m
#import "NSNumber+TurkishSpellOut.h"
@implementation NSNumber (TurkishSpellOut)
-(NSString*)spellOutTurkish
{
NSArray* ones = @[@"",@"bir",@"iki",@"üç",@"dört",@"beş",@"altı",@"yedi",@"sekiz",@"dokuz"];
NSArray* tens = @[@"",@"on",@"yirmi",@"otuz",@"kırk",@"elli",@"altmış",@"yetmiş",@"seksen",@"doksan"];
NSArray* mils = @[@"",@"bin",@"milyon",@"milyar",@"trilyon",@"katrilyon",@"kentilyon",@"seksilyon",@"septilyon"];
long long n = self.longLongValue; // number to be processed
NSString* r = @""; // return string
NSInteger c = 0; // counter
while (n > 0)
{
NSString* p=@""; // partial string
NSInteger d = n%10; // current digit value
if(c%3 == 0)
{
if(!(d == 1 && c/3 == 1) || n%1000 > 1) // prevent "bir bin"
p = ones[d];
if(n%1000 != 0) // add mille suffix ("bin", "milyon", "milyar"...) if at least one digit exists
p = [p stringByAppendingFormat:@" %@",mils[c/3]];
}
else if(c%3 == 1)
{
p = tens[d];
}
else if(c%3 == 2)
{
if(d > 1) // prevent "bir yüz"
p = [ones[d] stringByAppendingString:@" "];
if(d > 0)
p = [p stringByAppendingString:@"yüz"];
}
if (![p isEqualToString:@""])
r = [@" " stringByAppendingString:r];
r = [p stringByAppendingString:r];
c++;
n/=10;
}
// delete uncesssary spaces
r = [r stringByReplacingOccurrencesOfString:@" " withString:@" "];
r = [r stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
return r;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment