I've been searching forever for some sample code on convert from a number to roman numerals in objective c. Anyone know where I can find a good example?
Update:
Nevermind, found a PHP function that does what I want and ported it. Seems to work fine so far.
-(NSString*)numberToRomanNumerals:(int)num{
if (num < 0 || num > 9999) { return @""; } // out of range
NSArray *r_ones = [[NSArray alloc]initWithObjects:@"I", @"II", @"III", @"IV", @"V", @"VI", @"VII", @"VIII",@"IX",nil];
NSArray *r_tens = [[NSArray alloc]initWithObjects:@"X", @"XX", @"XXX", @"XL", @"L", @"LX", @"LXX",@"LXXX", @"XC",nil];
NSArray *r_hund = [[NSArray alloc]initWithObjects:@"C", @"CC", @"CCC", @"CD", @"D", @"DC", @"DCC",@"DCCC", @"CM",nil];
NSArray *r_thou = [[NSArray alloc]initWithObjects:@"M", @"MM", @"MMM", @"MMMM", @"MMMMM", @"MMMMMM",@"MMMMMMM",
@"MMMMMMMM", @"MMMMMMMMM",nil];
int ones = num % 10;
int tens = (num - ones) % 100;
int hundreds = (num - tens - ones) % 1000;
int thou = (num - hundreds - tens - ones) % 10000;
tens = tens / 10;
hundreds = hundreds / 100;
thou = thou / 1000;
NSString *rnum=@"";
if (thou) { rnum = [rnum stringByAppendingString:[r_thou objectAtIndex:thou-1]]; }
if (hundreds) { rnum = [rnum stringByAppendingString:[r_hund objectAtIndex:hundreds-1]]; }
if (tens) { rnum = [rnum stringByAppendingString:[r_tens objectAtIndex:tens-1]]; }
if (ones) { rnum = [rnum stringByAppendingString:[r_ones objectAtIndex:ones-1]]; }
[r_ones release];
[r_tens release];
[r_hund release];
[r_thou release];
return rnum;
}
Is this homework I wonder?
The algorithm for the conversion this is just a simple "reduce number, build up string" iteration of some sort and you only need basic math, comparison and string operations.
Go to Computer Algorithms where you will find various algorithms discussed and presented in VB - it is not hard to figure out the VB even if you don't know the language.