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

  • Is your sales and operations planning helping or hurting your bottom line? Here are 5 useful tips from the experts at Quintiq to guide you to a better S&OP strategy.

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today make data protection a must-have, as we live in a data driven society. The digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join eVault Chief Technology …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds