Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C#
Tip/Trick

Converting dates to the Roman equivalent

Rate me:
Please Sign up or sign in to vote.
4.67/5 (3 votes)
26 Aug 2010CPOL1 min read 20.1K   1   5
If your tutor asks you to convert years to the Roman equivalent, (e.g. 2010 to MMX) then you are not alone! Here is the simplest, most foolproof way to do it!
Tutors can be so unimaginative! Frequently they ask students to convert years to the Roman equivalent, and so dutifully you go off and re-invent the wheel. AGAIN.
Now, you won't have to! Just download a small ZIP file, decompress it to your My Documents folder, and add a small piece of code - easy! You might want to change the comments a bit, a few of the names perhaps to make it look like you wrote it, but Hey! That's easier than actually working out how to do it, isn't it?

Procedure: Download the conversion database here[^] and unzip it to your My Documents folder. Note that this conversion works from the founding of Rome (start of Ab Urbe Condita as you would expect) to the end of the Gregorian calender in 4246

To make it easier, this is an SQLCE database, which means it is small, compact and does not require SQL Server or MySQL to be installed - it works with the .NET SqlServerCe assembly which is installed with .NET automatically. Additionally, it means this will work with Windows CE or Windows Mobile!

Add a reference in your project to the "System.Data.SqlServerCe" assembly, and a "using System.Data.SqlServerCe;" to the top of your source file.
Then just paste the following code into your app and use the GetRomanDate method.

C#
/// <summary>
        /// Get the Roman eqivilant of a year (normal Gregorian calender)
        /// </summary>
        /// <param name="year">Year to convert</param>
        /// <returns>Roman numeral version of the year</returns>
        private string GetRomanDate(int year)
            {
            return GetRomanDate(year, false);       // Convert without Ab Urbe Condita
            }
        /// <summary>
        /// Get the Roman eqivilant of a year
        /// Normal Gregorian calender, or Ab Urbe Condita
        /// </summary>
        /// <param name="year">Year to convert</param>
        /// <param name="abUrbeCondita">True if Ab Urbe Condita convertion is required</param>
        /// <returns></returns>
        private string GetRomanDate(int year, bool abUrbeCondita)
            {
            // Connect to the DB
            using (SqlCeConnection con = new SqlCeConnection(@"Data Source=" + Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\RomanDates.sdf;Persist Security Info=True"))
                {
                con.Open();
                // Fetch the roman numeral string
                string sql = "SELECT * FROM Years WHERE Year=@YEAR";
                using (SqlCeCommand cmd = new SqlCeCommand(sql, con))
                    {
                    cmd.Parameters.AddWithValue("@YEAR", year);
                    using (SqlCeDataReader dr = cmd.ExecuteReader())
                        {
                        // And return the word version.
                        return (dr.Read() ? (string) dr[(abUrbeCondita ? "AbUrbeCondita" : "Roman")] : "Outside the bounds of time!");
                        }
                    }
                }
            }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
Bug[CRITICAL ERROR] Database no longer exists. Pin
Brisingr Aerowing24-Mar-15 13:34
professionalBrisingr Aerowing24-Mar-15 13:34 
Generalit was nice. helpful Pin
Hiren solanki29-Aug-10 20:02
Hiren solanki29-Aug-10 20:02 
QuestionCoding Horror? Pin
Bernhard Hiller31-Aug-10 2:13
Bernhard Hiller31-Aug-10 2:13 
AnswerRe: Coding Horror? Pin
OriginalGriff31-Aug-10 2:26
mveOriginalGriff31-Aug-10 2:26 
AnswerRe: Coding Horror? Pin
OriginalGriff31-Aug-10 2:26
mveOriginalGriff31-Aug-10 2:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.