'TODO
using System; using System.Diagnostics; #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> /// One, Two, Three, etc. /// </summary> English, /// <summary> /// No numbering /// </summary> None } #endregion public 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.English: result = ToEnglish(_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; } } //----------------------------------------------------------------------------------------- private static string ToEnglish(int value) { string result = ""; int mod10 = value % 10; int mod100 = value % 100; int mod1000 = value % 1000; if (value < 0 | value >= 1000000) { throw new OverflowException("Value must be a non-negative number less than one " + "million."); } else if (value < 20) { string[] values = new string[] {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; result = values[value]; } else if (value < 100) { if (mod10 == 0) { string[] values = new string[] {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; int index = value / 10 - 2; result = values[index]; } else { int multiple = value / 10 * 10; result = ToEnglish(multiple) + "-" + ToEnglish(mod10); } } else if (value < 1000) { int hundreds = value / 100; if (mod100 == 0) { result = ToEnglish(hundreds) + " Hundred"; } else { result = ToEnglish(hundreds) + " Hundred " + ToEnglish(mod10); } } else if (mod1000 == 0) { result = ToEnglish(value / 1000) + " Thousand"; } else { result = ToEnglish(value / 1000) + " Thousand " + ToEnglish(mod1000); } return result; } }
public class RomanNumeral { int _value; string _numeral; public RomanNumeral(int value, string numeral) { _value = value; _numeral = numeral; } public int Value { get { return _value; }} public string Numeral { get { return _numeral; }} }
ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.