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: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds