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: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds