Converting Decimal Numbers to Roman Numerals

Environment: C, Visual C++

The following function takes positive decimal numbers from 0–4999 and outputs them as Roman strings (for example, "MMIII" for 2003). It should compile under any version of C on any platform (although you may want to change "//" comments to the "/*...*/" form (which I hate).

I wrote the function myself, and I have no copyright concerns. I searched the CodeGuru site for "roman" and got nothing except font names, so I assume noone else has posted anything similar to this.

// written by Ste Cork, free for any and all use.
//
const char *Number_AsRomanString( int iNumber )
{
struct RomanDigit_t
  {
char *m_psString;
int m_iValue;
  };

static const RomanDigit_t RomanDigits[]=
  {
    {"M",  1000},
    {"CM",  900},
    {"D",   500},
    {"CD",  400},
    {"C",   100},
    {"XC",   90},
    {"L",    50},
    {"XL",   40},
    {"X",    10},
    {"IX",    9},
    {"V",     5},
    {"IV",    4},
    {"I",     1},
  };

// Strictly speaking, Roman digits can't display something
// such as 4999 without using overlaid bars and so forth,
// but for now this is a quick-and-dirty piece of code that'll
// just keep using M's...
//
static char sRomanString[20];
sRomanString[0] = '\0';

for (int i=0; iNumber && i<sizeof(RomanDigits)/
                           sizeof(RomanDigits[0]); i++)
  {
while ( RomanDigits[i].m_iValue <= iNumber )
    {
strcat( sRomanString, RomanDigits[i].m_psString );
iNumber -= RomanDigits[i].m_iValue;
    }
  }

return sRomanString;
}


Comments

  • *why* do people use roman numerals?

    Posted by Legacy on 07/29/2003 12:00am

    Originally posted by: njkayaker


    The roman empire is dead! Get over it.

    I really think roman numerals is something that should go away.

    Reply
  • Nice try.

    Posted by Legacy on 07/29/2003 12:00am

    Originally posted by: felix

    You only solved half of the problem. How about converting a roman numeral to a long? problem is starting to get a little harder now isn't it.

    Also, using your scheme, one million is basically 1000 M's.

    I realize that you do not support the *bar* notation, but it would have to be taken into account so that you solution could be useful.

    I would use long instead of int.


    -f

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • A help desk is critical to the operations of an IT services business. As a centralized intake location for technical issues, it allows for a responsive and timely solution to get clients and their staff back to business as usual. In addition to handling immediate IT issues, a help desk performs several proactive tasks to ensure clients' IT systems remain operational and downtime is minimized. Thus, utilizing a help desk and following best practices can improve the productivity, efficiency and satisfaction of …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds