Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Page History: Numeral Class

Compare Page Revisions



« Older Revision - Back to Page History - Newer Revision »


Page Revision: Tue, Oct 27, 2009, 8:46 PM


This page is a Draft. Its content is not complete and might contain errors.

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Diagnostics;

namespace ScrewTurnWiki.FormatProvider.Outline
{
    #region NumeralFormat enum
    public enum NumeralFormat
    {
        /// <summary>
        /// 1, 2, 3, 4, etc.
        /// </summary>
        Arabic,

        /// <summary>
        /// A, B, C, D, ... AA, BB, CC, DD, etc.
        /// </summary>
        Letter,

        /// <summary>
        /// I, II, III, IV, etc.
        /// </summary>
        Roman,

        /// <summary>
        /// No numbering
        /// </summary>
        None
    }
    #endregion

    internal class Numeral
    {
        //- Private Declarations ------------------------------------------------------------------
        private int _value = 0;

        //- Constructor and Public Members --------------------------------------------------------
        public Numeral(int value)
        {
            _value = value;
        }
        public Numeral ChangeValue(int newValue)
        {
            _value = newValue;
            return this;
        }
        public void DebugPrint(NumeralFormat format)
        {
            string result = "";
            try
            {
                result = this.ToString(format);
            }
            catch (Exception ex)
            {
                result = "ERROR: " + ex.Message;
            }
            Debug.Print("Conversion of [" + _value.ToString().PadLeft(5) + "] to ["
                + format.ToString().PadRight(13) + "] format = " + result);
        }
        public string ToString(NumeralFormat format)
        {
            string result = "";
            switch (format)
            {
                case NumeralFormat.Arabic:
                    result = _value.ToString();
                    break;
                case NumeralFormat.Letter:
                    result = ToLetter(_value);
                    break;
                case NumeralFormat.Roman:
                    result = ToRoman(_value);
                    break;
                case NumeralFormat.None:
                    //result = "";
                    break;
            }
            return result;
        }
        public int Value
        {
            get { return _value; }
            set { _value = value; }
        }

        //- Private Static Methods ----------------------------------------------------------------
        private static string ToLetter(int value)
        {
            try
            {
                string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                char letter = letters[value % 26 - 1];
                int count = (int)System.Math.Truncate((double)value / 26D) + 1;
                string result = letter.ToString().PadRight(count, letter);
                return result;
            }
            catch (Exception ex)
            {
                //Debug.Print(ex.Message);
                return "ERROR: " + ex.Message;
            }

        }
        private static string ToRoman(int value)
        {
            if (value >= 10000 | value <= 0)
            {
                throw new OverflowException("Only positive values less than 10,000 can be converted to roman numerals.");
            }
            else
            {
                RomanNumeral[] numerals = {
                    new RomanNumeral(1000, "M"),
                    new RomanNumeral( 900, "CM"),
                    new RomanNumeral( 500, "D"),
                    new RomanNumeral( 400, "CD"),
                    new RomanNumeral( 100, "C"),
                    new RomanNumeral(  90, "XC"),
                    new RomanNumeral(  50, "L"),
                    new RomanNumeral(  40, "XL"),
                    new RomanNumeral(  10, "X"),
                    new RomanNumeral(   9, "IX"),
                    new RomanNumeral(   5, "V"),
                    new RomanNumeral(   4, "IV"),
                    new RomanNumeral(   1, "I")

                };

                string result = "";
                foreach (RomanNumeral n in numerals)
                {
                    if (value > n.Value)
                    {
                        result += n.Numeral + ToRoman(value - n.Value);
                        break;
                    }                    
                    else if (value == n.Value)
                    {
                        result += n.Numeral;
                        break;
                    }                    
                }
                return result;
            }
        }

    }
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.