Click here to Skip to main content
Click here to Skip to main content
Go to top

Converting dates to the Roman equivalent

, 26 Aug 2010
Rate this:
Please Sign up or sign in to vote.
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.
 
/// <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)

Share

About the Author

OriginalGriff
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?
Follow on   Google+

Comments and Discussions

 
Generalit was nice. helpful PinmemberHiren Solanki29-Aug-10 20:02 
QuestionCoding Horror? Pinmemberbhiller31-Aug-10 2:13 
AnswerRe: Coding Horror? PinmemberOriginalGriff31-Aug-10 2:26 
AnswerRe: Coding Horror? PinmemberOriginalGriff31-Aug-10 2:26 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 26 Aug 2010
Article Copyright 2010 by OriginalGriff
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid