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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Live Event Date: September 17, 2014 @ 12:00 p.m. ET / 9:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds