• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Roman numerals NSNumberFormatter?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Roman numerals NSNumberFormatter?


  • Subject: Re: Roman numerals NSNumberFormatter?
  • From: Antonio Nunes <email@hidden>
  • Date: Fri, 27 Oct 2006 15:15:01 +0100

On 27 Oct 2006, at 14:28, Keith Blount wrote:

I am probably missing something really obvious here (I usually am), but is there any easy way to convert an NSNumber (or just a regular int) to a roman numeral? NSNumberFormatter seemed the obvious place to look for this, but although it has everything else - including the very useful NSNumberFormatterSpellOutStyle - I can't see any style for converting to roman numerals.

Does anybody know how to do this? Do I have to write my own formatter, am I missing one that exists, or is there an easier, more basic way of doing this that I am missing?

Hi Keith,

I don't know whether this is easy, or even a particularly good way to do it, but it certainly works for me:

I set these two up when my program is loaded:
NSDictionary *romanNumeralPairs = [[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"RomanNumerals" ofType:@"plist"]] retain];
NSArray *romanNumeralPairValues = [[NSArray arrayWithObjects:@"1000", @"900", @"500", @"400", @"100", @"90", @"50", @"40", @"10", @"9", @"5", @"4", @"1", nil] retain];


The file contents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http:// www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>1</key>
<string>i</string>
<key>10</key>
<string>x</string>
<key>100</key>
<string>c</string>
<key>1000</key>
<string>m</string>
<key>4</key>
<string>iv</string>
<key>40</key>
<string>xl</string>
<key>400</key>
<string>cd</string>
<key>5</key>
<string>v</string>
<key>50</key>
<string>l</string>
<key>500</key>
<string>d</string>
<key>9</key>
<string>ix</string>
<key>90</key>
<string>xc</string>
<key>900</key>
<string>cm</string>
</dict>
</plist>


Then I can use them in this method...

+ (NSString *)convertArabicToRomanNumeralValue:(int)input
{
int i, deflate = input;
NSString *romanValue = @"";
NSDictionary *pairs = [[NSApp delegate] romanNumeralPairs];
NSArray *values = [[NSApp delegate] romanNumeralPairValues];
int itemCount = [a count], itemValue;

for (i = 0; i < itemCount; i++) {
itemValue = [[values objectAtIndex:i] intValue];

while (deflate >= itemValue) {
romanValue = [romanValue stringByAppendingString:[paris objectForKey:[values objectAtIndex:i]]];
deflate -= itemValue;
}
}
return romanValue;
}


... and get back the desired string. It's easy to convert the result to uppercase if required.

HPH,
António

-----------------------------------------
Forgiveness is not an occasional act;
it is a permanent attitude.

--Martin Luther King, Jr
-----------------------------------------




_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Roman numerals NSNumberFormatter? (From: Keith Blount <email@hidden>)

  • Prev by Date: Xcode project template for Game Developers
  • Next by Date: Re: Seeking debugging advice
  • Previous by thread: Roman numerals NSNumberFormatter?
  • Next by thread: Re: Roman numerals NSNumberFormatter?
  • Index(es):
    • Date
    • Thread