A couple of weeks ago I needed to spell numbers for a project that I am currently working on. Today I thought that it could be a good idea to share code for spelling numbers in C# and .Net in couple of languages (Bulgarian, Russian, Spanish, Macedonian, English, Afrikaans). So here it goes:
#region Bulgarian numbers
/// <summary>
/// Converts digits to bulgarian words.
/// </summary>
/// <param name="value">int64</param>
/// <returns>string</returns>
private static string ConvertDigitBulgarian(Int64 value)
{
string str = String.Empty;
if (value < 0)
{
return string.Concat("минус ", ConvertDigitBulgarian(Math.Abs(value)));
}
if (value == 0)
{
str = "нула";
}
else if (value == 1)
{
str = "едно";
}
else if (value == 2)
{
str = "две";
}
else if (value == 3)
{
str = "три";
}
else if (value == 4)
{
str = "четири";
}
else if (value == 5)
{
str = "пет";
}
else if (value == 6)
{
str = "шест";
}
else if (value == 7)
{
str = "седем";
}
else if (value == 8)
{
str = "осем";
}
else if (value == 9)
{
str = "девет";
}
else if (value == 10)
{
str = "десет";
}
else if (value == 11)
{
str = "единадесет";
}
else if (value == 12)
{
str = "дванадесет";
}
else if (value == 13)
{
str = "тринадесет";
}
else if (value == 14)
{
str = "четиринадесет";
}
else if (value == 15)
{
str = "петнадесет";
}
else if (value == 16)
{
str = "шестнадесет";
}
else if (value == 17)
{
str = "седемнадесет";
}
else if (value == 18)
{
str = "осемнадесет";
}
else if (value == 19)
{
str = "деветнадесет";
}
else if (value < 20)
{
str = string.Concat(" двадесет", ConvertDigitBulgarian(value - 10));
}
else if (value == 20)
{
str = " двадесет";
}
else if (value < 30)
{
str = string.Concat(" двадесет и ", ConvertDigitBulgarian(value - 20));
}
else if (value == 30)
{
str = " тридесет";
}
else if (value < 40)
{
str = string.Concat(" тридесет и ", ConvertDigitBulgarian(value - 30));
}
else if (value == 40)
{
str = " четиридесет";
}
else if (value < 50)
{
str = string.Concat(" четиридесет и ", ConvertDigitBulgarian(value - 40));
}
else if (value == 50)
{
str = " петдесет";
}
else if (value < 60)
{
str = string.Concat(" петдесет и ", ConvertDigitBulgarian(value - 50));
}
else if (value == 60)
{
str = " шестдесет";
}
else if (value < 70)
{
str = string.Concat(" шестдесет и ", ConvertDigitBulgarian(value - 60));
}
else if (value == 70)
{
str = " седемдесет";
}
else if (value < 80)
{
str = string.Concat(" седемдесет и ", ConvertDigitBulgarian(value - 70));
}
else if (value == 80)
{
str = " осемдесет";
}
else if (value < 90)
{
str = string.Concat(" осемдесет и ", ConvertDigitBulgarian(value - 80));
}
else if (value == 90)
{
str = " деветдесет";
}
else if (value < 100)
{
str = string.Concat(" деветдесет и ", ConvertDigitBulgarian(value - 90));
}
else if (value < 100)
{
Int64 num = value % 10;
str = string.Format("{0} {1}", ConvertDigitBulgarian(value / 10 * 10), (num == 1 ? "сто и" : ConvertDigitBulgarian(value % 10)));
}
else if (value == 100)
{
str = " сто";
}
else if (value < 200)
{
str = string.Concat(" сто и ", ConvertDigitBulgarian(value - 100));
}
else if (value == 200 || value == 300)
{
str = string.Concat(ConvertDigitBulgarian(value / 100), "ста");
}
else if (value == 400 || value == 600 || value == 800 || value == 500 || value == 700 || value == 900)
{
str = string.Concat(ConvertDigitBulgarian(value / 100), "стотин");
}
else if (value < 1000)
{
str = string.Format("{0} {1}", ConvertDigitBulgarian(value / 100 * 100), ConvertDigitBulgarian(value % 100));
}
else if (value == 1000)
{
str = " хиляда";
}
else if (value < 2000)
{
str = string.Concat(" хиляда и ", ConvertDigitBulgarian(value % 1000));
}
else if (value < 1000000)
{
str = string.Concat(ConvertDigitBulgarian(value / 1000), " хиляди");
if (value % 1000 > 0)
{
str = string.Concat(str, " ", ConvertDigitBulgarian(value % 1000));
}
}
else if (value == 1000000)
{
str = " един милион";
}
else if (value < 2000000)
{
str = string.Concat(" един милион ", ConvertDigitBulgarian(value % 1000000));
}
else if (value < 1000000000)
{
str = string.Concat(ConvertDigitBulgarian(value / 1000000), " милиона");
if (value - value / 1000000 * 1000000 > 0)
{
str = string.Concat(str, " ", ConvertDigitBulgarian(value - value / 1000000 * 1000000));
}
}
else if (value == 1000000000)
{
str = " един милиард";
}
else if (value < 2000000000)
{
str = string.Concat("един милиард ", ConvertDigitBulgarian(value % 1000000000));
}
else if (value < 1000000000000)
{
str = string.Concat(ConvertDigitBulgarian(value / 1000000000), " милиарда");
if (value - value / 1000000000 * 1000000000 > 0)
{
str = string.Concat(str, " ", ConvertDigitBulgarian(value - value / 1000000000 * 1000000000));
}
}
else if (value == 1000000000000)
{
str = " един трилион";
}
else if (value < 2000000000000)
{
str = string.Concat(" един трилион ", ConvertDigitBulgarian(value % 1000000000000));
}
else if (value < 1000000000000000)
{
str = string.Concat(ConvertDigitBulgarian(value / 1000000000000), " трилиона");
if (value - value / 1000000000000 * 1000000000000 > 0)
{
str = string.Concat(str, " ", ConvertDigitBulgarian(value - value / 1000000000000 * 1000000000000));
}
}
return str;
}
/// <summary>
/// Spells numbers in blugarian language.
/// </summary>
/// <param name="Value"></param>
/// <returns>string</returns>
private static string SpellNumberInBulgarian(double Value)
{
Int64 num = (Int64)Value;
string str = Value.ToString("0.00#");
char[] chrArray = new char[] { ',' };
string str1 = str.Split(chrArray)[1];
Int64 num1 = Int64.Parse(str1);
if (num1 == 0)
{
return ConvertDigitBulgarian(num);
}
return string.Format("{0} точка {1} ", ConvertDigitBulgarian(num), ConvertDigitBulgarian(num1));
}
#endregion
#region Russian numbers
/// <summary>
/// Converts digits to russian words.
/// </summary>
/// <param name="value">int64</param>
/// <returns>string</returns>
private static string ConvertDigitRussian(Int64 value)
{
string str = String.Empty;
if (value < 0)
{
return string.Concat("минус ", ConvertDigitRussian(Math.Abs(value)));
}
if (value == 0)
{
str = "нуль";
}
else if (value == 1)
{
str = "один";
}
else if (value == 2)
{
str = "два";
}
else if (value == 3)
{
str = "три";
}
else if (value == 4)
{
str = "четыре";
}
else if (value == 5)
{
str = "пять";
}
else if (value == 6)
{
str = "шесть";
}
else if (value == 7)
{
str = "семь";
}
else if (value == 8)
{
str = "восемь";
}
else if (value == 9)
{
str = "девять";
}
else if (value == 10)
{
str = "десять";
}
else if (value == 11)
{
str = "одиннадцать";
}
else if (value == 12)
{
str = "двенадцать";
}
else if (value == 13)
{
str = "тринадцать";
}
else if (value == 14)
{
str = "четырнадцать";
}
else if (value == 15)
{
str = "пятнадцать";
}
else if (value == 16)
{
str = "шестнадцать";
}
else if (value == 17)
{
str = "семнадцать";
}
else if (value == 18)
{
str = "восемнадцать";
}
else if (value == 19)
{
str = "девятнадцать";
}
else if (value < 20)
{
str = string.Concat(" двадцать", ConvertDigitRussian(value - 10));
}
else if (value == 20)
{
str = " двадцать";
}
else if (value < 30)
{
str = string.Concat(" двадцать ", ConvertDigitRussian(value - 20));
}
else if (value == 30)
{
str = " тридцать";
}
else if (value < 40)
{
str = string.Concat(" тридцать ", ConvertDigitRussian(value - 30));
}
else if (value == 40)
{
str = " сорок";
}
else if (value < 50)
{
str = string.Concat(" сорок ", ConvertDigitRussian(value - 40));
}
else if (value == 50)
{
str = " пятьдесят";
}
else if (value < 60)
{
str = string.Concat(" пятьдесят ", ConvertDigitRussian(value - 50));
}
else if (value == 60)
{
str = " шестьдесят";
}
else if (value < 70)
{
str = string.Concat(" шестьдесят ", ConvertDigitRussian(value - 60));
}
else if (value == 70)
{
str = " семьдесят";
}
else if (value < 80)
{
str = string.Concat(" семьдесят ", ConvertDigitRussian(value - 70));
}
else if (value == 80)
{
str = " восемьдесят";
}
else if (value < 90)
{
str = string.Concat(" восемьдесят ", ConvertDigitRussian(value - 80));
}
else if (value == 90)
{
str = " девяносто";
}
else if (value < 100)
{
str = string.Concat(" девяносто ", ConvertDigitRussian(value - 90));
}
else if (value < 100)
{
Int64 num = value % 10;
str = string.Format("{0} {1}", ConvertDigitRussian(value / 10 * 10), (num == 1 ? "сто " : ConvertDigitRussian(value % 10)));
}
else if (value == 100)
{
str = " сто";
}
else if (value < 200)
{
str = string.Concat(" сто ", ConvertDigitRussian(value - 100));
}
else if (value == 200)
{
str = " двести";
}
else if (value < 300)
{
str = string.Concat(" двести ", ConvertDigitRussian(value - 200));
}
else if (value == 300)
{
str = " триста";
}
else if (value < 400)
{
str = string.Concat(" триста ", ConvertDigitRussian(value - 300));
}
else if (value == 400)
{
str = " четыреста";
}
else if (value < 500)
{
str = string.Concat(" четыреста ", ConvertDigitRussian(value - 400));
}
else if (value == 500 || value == 600 || value == 700 || value == 800 || value == 900)
{
str = string.Concat(ConvertDigitRussian(value / 100), "сот");
}
else if (value < 1000)
{
str = string.Format("{0} {1}", ConvertDigitRussian(value / 100 * 100), ConvertDigitRussian(value % 100));
}
else if (value == 1000)
{
str = " одна тысяча";
}
else if (value < 2000)
{
str = string.Concat(" одна тысяча ", ConvertDigitRussian(value % 1000));
}
else if (value < 1000000)
{
str = string.Concat(ConvertDigitRussian(value / 1000), " тысяч");
if (value % 1000 > 0)
{
str = string.Concat(str, " ", ConvertDigitRussian(value % 1000));
}
}
else if (value == 1000000)
{
str = " один миллион";
}
else if (value < 2000000)
{
str = string.Concat(" один миллион ", ConvertDigitRussian(value % 1000000));
}
else if (value < 1000000000)
{
str = string.Concat(ConvertDigitRussian(value / 1000000), " миллиона");
if (value - value / 1000000 * 1000000 > 0)
{
str = string.Concat(str, " ", ConvertDigitRussian(value - value / 1000000 * 1000000));
}
}
else if (value == 1000000000)
{
str = " один миллиард";
}
else if (value < 2000000000)
{
str = string.Concat("один миллиард ", ConvertDigitRussian(value % 1000000000));
}
else if (value < 1000000000000)
{
str = string.Concat(ConvertDigitRussian(value / 1000000000), " миллиарда");
if (value - value / 1000000000 * 1000000000 > 0)
{
str = string.Concat(str, " ", ConvertDigitRussian(value - value / 1000000000 * 1000000000));
}
}
else if (value == 1000000000000)
{
str = " один триллион";
}
else if (value < 2000000000000)
{
str = string.Concat(" один триллион ", ConvertDigitRussian(value % 1000000000000));
}
else if (value < 1000000000000000)
{
str = string.Concat(ConvertDigitRussian(value / 1000000000000), " триллиона");
if (value - value / 1000000000000 * 1000000000000 > 0)
{
str = string.Concat(str, " ", ConvertDigitRussian(value - value / 1000000000000 * 1000000000000));
}
}
return str;
}
/// <summary>
/// Spells numbers in russian language.
/// </summary>
/// <param name="Value"></param>
/// <returns>string</returns>
private static string SpellNumberInRussian(double Value)
{
Int64 num = (Int64)Value;
string str = Value.ToString("0.00#");
char[] chrArray = new char[] { ',' };
string str1 = str.Split(chrArray)[1];
Int64 num1 = Int64.Parse(str1);
if (num1 == 0)
{
return ConvertDigitRussian(num);
}
return string.Format("{0} точка {1} ", ConvertDigitRussian(num), ConvertDigitRussian(num1));
}
#endregion
#region Spanish numbers
/// <summary>
/// Converts digits to spanish words.
/// </summary>
/// <param name="value">int64</param>
/// <returns>string</returns>
private static string ConvertDigitSpanish(Int64 value)
{
string str = String.Empty;
if (value < 0)
{
return string.Concat("menos ", ConvertDigitSpanish(Math.Abs(value)));
}
if (value == 0)
{
str = "cero";
}
else if (value == 1)
{
str = "uno";
}
else if (value == 2)
{
str = "dos";
}
else if (value == 3)
{
str = "tres";
}
else if (value == 4)
{
str = "cuatro";
}
else if (value == 5)
{
str = "cinco";
}
else if (value == 6)
{
str = "seis";
}
else if (value == 7)
{
str = "siete";
}
else if (value == 8)
{
str = "ocho";
}
else if (value == 9)
{
str = "nueve";
}
else if (value == 10)
{
str = "diez";
}
else if (value == 11)
{
str = "once";
}
else if (value == 12)
{
str = "doce";
}
else if (value == 13)
{
str = "trece";
}
else if (value == 14)
{
str = "catorce";
}
else if (value == 15)
{
str = "quince";
}
else if (value == 16)
{
str = "dieciséis";
}
else if (value < 20)
{
str = string.Concat("dieci", ConvertDigitSpanish(value - 10));
}
else if (value == 20)
{
str = "veinte";
}
else if (value < 30)
{
str = string.Concat("veinti", ConvertDigitSpanish(value - 20));
}
else if (value == 30)
{
str = "treinta";
}
else if (value == 40)
{
str = "cuarenta";
}
else if (value == 50)
{
str = "cincuenta";
}
else if (value == 60)
{
str = "sesenta";
}
else if (value == 70)
{
str = "setenta";
}
else if (value == 80)
{
str = "ochenta";
}
else if (value == 90)
{
str = "noventa";
}
else if (value < 100)
{
Int64 num = value % 10;
str = string.Format("{0} y {1}", ConvertDigitSpanish(value / 10 * 10), (num == 1 ? "un" : ConvertDigitSpanish(value % 10)));
}
else if (value == 100)
{
str = "cien";
}
else if (value < 200)
{
str = string.Concat("ciento ", ConvertDigitSpanish(value - 100));
}
else if (value == 200 || value == 300 || value == 400 || value == 600 || value == 800)
{
str = string.Concat(ConvertDigitSpanish(value / 100), "cientos");
}
else if (value == 500)
{
str = "quinientos";
}
else if (value == 700)
{
str = "setecientos";
}
else if (value == 900)
{
str = "novecientos";
}
else if (value < 1000)
{
str = string.Format("{0} {1}", ConvertDigitSpanish(value / 100 * 100), ConvertDigitSpanish(value % 100));
}
else if (value == 1000)
{
str = "mil";
}
else if (value < 2000)
{
str = string.Concat("mil ", ConvertDigitSpanish(value % 1000));
}
else if (value < 1000000)
{
str = string.Concat(ConvertDigitSpanish(value / 1000), " mil");
if (value % 1000 > 0)
{
str = string.Concat(str, " ", ConvertDigitSpanish(value % 1000));
}
}
else if (value == 1000000)
{
str = "un millón";
}
else if (value < 2000000)
{
str = string.Concat("un millón ", ConvertDigitSpanish(value % 1000000));
}
else if (value < 1000000000)
{
str = string.Concat(ConvertDigitSpanish(value / 1000000), " millones");
if (value - value / 1000000 * 1000000 > 0)
{
str = string.Concat(str, " ", ConvertDigitSpanish(value - value / 1000000 * 1000000));
}
}
else if (value == 1000000000)
{
str = "un billón";
}
else if (value < 2000000000)
{
str = string.Concat("un billón ", ConvertDigitSpanish(value % 1000000000));
}
else if (value < 1000000000000)
{
str = string.Concat(ConvertDigitSpanish(value / 1000000000), " billónes");
if (value - value / 1000000000 * 1000000000 > 0)
{
str = string.Concat(str, " ", ConvertDigitSpanish(value - value / 1000000000 * 1000000000));
}
}
else if (value == 1000000000000)
{
str = "un trillón";
}
else if (value < 2000000000000)
{
str = string.Concat("un trillón ", ConvertDigitSpanish(value % 1000000000000));
}
else if (value < 1000000000000000)
{
str = string.Concat(ConvertDigitSpanish(value / 1000000000000), " trillónes");
if (value - value / 1000000000000 * 1000000000000 > 0)
{
str = string.Concat(str, " ", ConvertDigitSpanish(value - value / 1000000000000 * 1000000000000));
}
}
return str;
}
/// <summary>
/// Spells numbers in spanish language.
/// </summary>
/// <param name="Value"></param>
/// <returns>string</returns>
private static string SpellNumberInSpanish(double Value)
{
Int64 num = (Int64)Value;
string str = Value.ToString("0.00#");
char[] chrArray = new char[] { ',' };
string str1 = str.Split(chrArray)[1];
Int64 num1 = Int64.Parse(str1);
if (num1 == 0)
{
return ConvertDigitSpanish(num);
}
return string.Format("{0} coma {1} ", ConvertDigitSpanish(num), ConvertDigitSpanish(num1));
}
#endregion
#region Macedonian numbers
/// <summary>
/// Converts digits to macedonian words.
/// </summary>
/// <param name="value">int64</param>
/// <returns>string</returns>
private static string ConvertDigitMacedonian(Int64 value)
{
string str = String.Empty;
if (value < 0)
{
return string.Concat("минус ", ConvertDigitMacedonian(Math.Abs(value)));
}
if (value == 0)
{
str = "нула";
}
else if (value == 1)
{
str = "еден";
}
else if (value == 2)
{
str = "два";
}
else if (value == 3)
{
str = "три";
}
else if (value == 4)
{
str = "четири";
}
else if (value == 5)
{
str = "пет";
}
else if (value == 6)
{
str = "шест";
}
else if (value == 7)
{
str = "седум";
}
else if (value == 8)
{
str = "осум";
}
else if (value == 9)
{
str = "девет";
}
else if (value == 10)
{
str = "десет";
}
else if (value == 11)
{
str = "единаесет";
}
else if (value == 12)
{
str = "дванаесет";
}
else if (value == 13)
{
str = "тринаесет";
}
else if (value == 14)
{
str = "четиринаесет";
}
else if (value == 15)
{
str = "петнаесет";
}
else if (value == 16)
{
str = "шеснаесет";
}
else if (value == 17)
{
str = "седумнаесет";
}
else if (value == 18)
{
str = "осумнаесет";
}
else if (value == 19)
{
str = "деветнаесет";
}
else if (value == 20)
{
str = "дваесет";
}
else if (value < 30)
{
str = string.Concat(" дваесет и ", ConvertDigitMacedonian(value - 20));
}
else if (value == 30)
{
str = " триесет";
}
else if (value < 40)
{
str = string.Concat(" триесет и ", ConvertDigitMacedonian(value - 30));
}
else if (value == 40)
{
str = " четириесет";
}
else if (value < 50)
{
str = string.Concat(" четириесет и ", ConvertDigitMacedonian(value - 40));
}
else if (value == 50)
{
str = " педесет";
}
else if (value < 60)
{
str = string.Concat(" педесет и ", ConvertDigitMacedonian(value - 50));
}
else if (value == 60)
{
str = " шеесет";
}
else if (value < 70)
{
str = string.Concat(" шеесет и ", ConvertDigitMacedonian(value - 60));
}
else if (value == 70)
{
str = " седумдесет";
}
else if (value < 80)
{
str = string.Concat(" седумдесет и ", ConvertDigitMacedonian(value - 70));
}
else if (value == 80)
{
str = " осумдесет";
}
else if (value < 90)
{
str = string.Concat(" осумдесет и ", ConvertDigitMacedonian(value - 80));
}
else if (value == 90)
{
str = " деведесет";
}
else if (value < 100)
{
str = string.Concat(" деведесет и ", ConvertDigitMacedonian(value - 90));
}
else if (value == 100)
{
str = " сто";
}
else if (value < 200)
{
str = string.Concat(" сто", ConvertDigitMacedonian(value - 100));
}
else if (value == 200)
{
str = " двесте";
}
else if (value < 300)
{
str = string.Concat(" двесте ", ConvertDigitMacedonian(value - 200));
}
else if (value == 300)
{
str = " триста";
}
else if (value < 400)
{
str = string.Concat(" триста ", ConvertDigitMacedonian(value - 300));
}
else if (value == 400 || value == 500 || value == 600 || value == 700 || value == 800 || value == 900)
{
str = string.Concat(ConvertDigitMacedonian(value / 100), "стотини");
}
else if (value < 1000)
{
str = string.Format("{0}{1}", ConvertDigitMacedonian(value / 100 * 100), ConvertDigitMacedonian(value % 100));
}
else if (value == 1000)
{
str = " илјада";
}
else if (value < 2000)
{
str = string.Concat(" илјада ", ConvertDigitMacedonian(value % 1000));
}
else if (value < 1000000)
{
str = string.Concat(ConvertDigitMacedonian(value / 1000), " илјади");
if (value % 1000 > 0)
{
str = string.Concat(str, " и ", ConvertDigitMacedonian(value % 1000));
}
}
else if (value == 1000000)
{
str = " милион";
}
else if (value < 2000000)
{
str = string.Concat(" милион и ", ConvertDigitMacedonian(value % 1000000));
}
else if (value < 1000000000)
{
str = string.Concat(ConvertDigitMacedonian(value / 1000000), " милиони");
if (value - value / 1000000 * 1000000 > 0)
{
str = string.Concat(str, " ", ConvertDigitMacedonian(value - value / 1000000 * 1000000));
}
}
else if (value == 1000000000)
{
str = " милијарда";
}
else if (value < 2000000000)
{
str = string.Concat(" милијарда и ", ConvertDigitMacedonian(value % 1000000000));
}
else if (value < 1000000000000)
{
str = string.Concat(ConvertDigitMacedonian(value / 1000000000), " милијарди");
if (value - value / 1000000000 * 1000000000 > 0)
{
str = string.Concat(str, " ", ConvertDigitMacedonian(value - value / 1000000000 * 1000000000));
}
}
else if (value == 1000000000000)
{
str = " трилион";
}
else if (value < 2000000000000)
{
str = string.Concat(" трилион и ", ConvertDigitMacedonian(value % 1000000000000));
}
else if (value < 1000000000000000)
{
str = string.Concat(ConvertDigitMacedonian(value / 1000000000000), " трилиони");
if (value - value / 1000000000000 * 1000000000000 > 0)
{
str = string.Concat(str, " ", ConvertDigitMacedonian(value - value / 1000000000000 * 1000000000000));
}
}
return str;
}
/// <summary>
/// Spells numbers in macedonian language.
/// </summary>
/// <param name="Value"></param>
/// <returns>string</returns>
private static string SpellNumberInMacedonian(double Value)
{
Int64 num = (Int64)Value;
string str = Value.ToString("0.00#");
char[] chrArray = new char[] { ',' };
string str1 = str.Split(chrArray)[1];
Int64 num1 = Int64.Parse(str1);
if (num1 == 0)
{
return ConvertDigitMacedonian(num);
}
return string.Format("{0} точка { 1} ", ConvertDigitMacedonian(num), ConvertDigitMacedonian(num1));
}
#endregion
#region English numbers
/// <summary>
/// Converts digits to english words.
/// </summary>
/// <param name="value">int64</param>
/// <returns>string</returns>
private static string ConvertDigitEnglish(Int64 value)
{
string str = String.Empty;
if (value < 0)
{
return string.Concat("minus ", ConvertDigitEnglish(Math.Abs(value)));
}
if (value == 0)
{
str = "zero";
}
else if (value == 1)
{
str = "one";
}
else if (value == 2)
{
str = "two";
}
else if (value == 3)
{
str = "three";
}
else if (value == 4)
{
str = "four";
}
else if (value == 5)
{
str = "five";
}
else if (value == 6)
{
str = "six";
}
else if (value == 7)
{
str = "seven";
}
else if (value == 8)
{
str = "eight";
}
else if (value == 9)
{
str = "nine";
}
else if (value == 10)
{
str = "ten";
}
else if (value == 11)
{
str = "eleven";
}
else if (value == 12)
{
str = "twelve";
}
else if (value == 13)
{
str = "thirteen";
}
else if (value == 14)
{
str = "fourteen";
}
else if (value == 15)
{
str = "fifteen";
}
else if (value == 16)
{
str = "sixteen";
}
else if (value == 17)
{
str = "seventeen";
}
else if (value == 18)
{
str = "eighteen";
}
else if (value == 19)
{
str = "nineteen";
}
else if (value < 20)
{
str = string.Concat(" twenty", ConvertDigitEnglish(value - 10));
}
else if (value == 20)
{
str = " twenty";
}
else if (value < 30)
{
str = string.Concat(" twenty", ConvertDigitEnglish(value - 20));
}
else if (value == 30)
{
str = " thirty";
}
else if (value == 40)
{
str = " forty";
}
else if (value == 50)
{
str = " fifty";
}
else if (value == 60)
{
str = " sixty";
}
else if (value == 70)
{
str = " seventy";
}
else if (value == 80)
{
str = " eighty";
}
else if (value == 90)
{
str = " ninety";
}
else if (value < 100)
{
Int64 num = value % 10;
str = string.Format("{0}-{1}", ConvertDigitEnglish(value / 10 * 10), (num == 1 ? "one" : ConvertDigitEnglish(value % 10)));
}
else if (value == 100)
{
str = " one hundred";
}
else if (value < 200)
{
str = string.Concat(" one hundred and ", ConvertDigitEnglish(value - 100));
}
else if (value == 200 || value == 300 || value == 400 || value == 600 || value == 800 || value == 500 || value == 700 || value == 900)
{
str = string.Concat(ConvertDigitEnglish(value / 100), " hundred");
}
else if (value < 1000)
{
str = string.Format("{0} {1}", ConvertDigitEnglish(value / 100 * 100), ConvertDigitEnglish(value % 100));
}
else if (value == 1000)
{
str = " one thousand";
}
else if (value < 2000)
{
str = string.Concat(" one thousand ", ConvertDigitEnglish(value % 1000));
}
else if (value < 1000000)
{
str = string.Concat(ConvertDigitEnglish(value / 1000), " thousand");
if (value % 1000 > 0)
{
str = string.Concat(str, " ", ConvertDigitEnglish(value % 1000));
}
}
else if (value == 1000000)
{
str = " one million";
}
else if (value < 2000000)
{
str = string.Concat(" one million ", ConvertDigitEnglish(value % 1000000));
}
else if (value < 1000000000)
{
str = string.Concat(ConvertDigitEnglish(value / 1000000), " millions");
if (value - value / 1000000 * 1000000 > 0)
{
str = string.Concat(str, " ", ConvertDigitEnglish(value - value / 1000000 * 1000000));
}
}
else if (value == 1000000000)
{
str = " one billion";
}
else if (value < 2000000000)
{
str = string.Concat("one billion ", ConvertDigitEnglish(value % 1000000000));
}
else if (value < 1000000000000)
{
str = string.Concat(ConvertDigitEnglish(value / 1000000000), " billions");
if (value - value / 1000000000 * 1000000000 > 0)
{
str = string.Concat(str, " ", ConvertDigitEnglish(value - value / 1000000000 * 1000000000));
}
}
else if (value == 1000000000000)
{
str = " one trillion";
}
else if (value < 2000000000000)
{
str = string.Concat(" one trillion ", ConvertDigitEnglish(value % 1000000000000));
}
else if (value < 1000000000000000)
{
str = string.Concat(ConvertDigitEnglish(value / 1000000000000), " trillions");
if (value - value / 1000000000000 * 1000000000000 > 0)
{
str = string.Concat(str, " ", ConvertDigitEnglish(value - value / 1000000000000 * 1000000000000));
}
}
return str;
}
/// <summary>
/// Spells numbers in english language.
/// </summary>
/// <param name="Value"></param>
/// <returns>string</returns>
private static string SpellNumberInEnglish(double Value)
{
Int64 num = (Int64)Value;
string str = Value.ToString("0.00#");
char[] chrArray = new char[] { ',' };
string str1 = str.Split(chrArray)[1];
Int64 num1 = Int64.Parse(str1);
if (num1 == 0)
{
return ConvertDigitEnglish(num);
}
return string.Format("{0} dot {1} ", ConvertDigitEnglish(num), ConvertDigitEnglish(num1));
}
#endregion
#region Afrikaans numbers
/// <summary>
/// Converts digits to afrikaans words.
/// </summary>
/// <param name="value">int64</param>
/// <returns>string</returns>
private static string ConvertDigitAfrikaans(Int64 value)
{
string str = String.Empty;
if (value < 0)
{
return string.Concat("minus ", ConvertDigitAfrikaans(Math.Abs(value)));
}
if (value == 0)
{
str = "nul";
}
else if (value == 1)
{
str = "een";
}
else if (value == 2)
{
str = "twee";
}
else if (value == 3)
{
str = "drie";
}
else if (value == 4)
{
str = "vier";
}
else if (value == 5)
{
str = "vyf";
}
else if (value == 6)
{
str = "ses";
}
else if (value == 7)
{
str = "sewe";
}
else if (value == 8)
{
str = "agt";
}
else if (value == 9)
{
str = "nege";
}
else if (value == 10)
{
str = "tien";
}
else if (value == 11)
{
str = "elf";
}
else if (value == 12)
{
str = "twaalf";
}
else if (value == 13)
{
str = "dertien";
}
else if (value == 14)
{
str = "veertien";
}
else if (value == 15)
{
str = "vyftien";
}
else if (value == 16)
{
str = "sestien";
}
else if (value == 17)
{
str = "sewentien";
}
else if (value == 18)
{
str = "agtien";
}
else if (value == 19)
{
str = "negentien";
}
else if (value == 20)
{
str = "twintig";
}
else if (value < 30)
{
str = string.Concat(ConvertDigitAfrikaans(value - 20), " en twintig");
}
else if (value == 30)
{
str = " dertig";
}
else if (value < 40)
{
str = string.Concat(ConvertDigitAfrikaans(value - 30), " en dertig");
}
else if (value == 40)
{
str = " veertig";
}
else if (value < 50)
{
str = string.Concat(ConvertDigitAfrikaans(value - 40), " en veertig");
}
else if (value == 50)
{
str = " vyftig";
}
else if (value < 60)
{
str = string.Concat(ConvertDigitAfrikaans(value - 50), " en vyftig");
}
else if (value == 60)
{
str = " sestig";
}
else if (value < 70)
{
str = string.Concat(ConvertDigitAfrikaans(value - 60), " en sestig");
}
else if (value == 70)
{
str = " sewentig";
}
else if (value < 80)
{
str = string.Concat(ConvertDigitAfrikaans(value - 70), " en sewentig");
}
else if (value == 80)
{
str = " tagtig";
}
else if (value < 90)
{
str = string.Concat(ConvertDigitAfrikaans(value - 80), " en tagtig");
}
else if (value == 90)
{
str = " negentig";
}
else if (value < 100)
{
str = string.Concat(ConvertDigitAfrikaans(value - 90), " en negentig");
}
else if (value == 100)
{
str = " een honderd";
}
else if (value < 200)
{
str = string.Concat(" een honderd ", ConvertDigitAfrikaans(value - 100));
}
else if (value == 300 || value == 400 || value == 500 || value == 600 || value == 700 || value == 800 || value == 900)
{
str = string.Concat(ConvertDigitAfrikaans(value / 100), " honderd");
}
else if (value < 1000)
{
str = string.Format("{0} en {1}", ConvertDigitAfrikaans(value / 100 * 100), ConvertDigitAfrikaans(value % 100));
}
else if (value == 1000)
{
str = " een duisend";
}
else if (value < 2000)
{
str = string.Concat("een duisend ", ConvertDigitAfrikaans(value % 1000));
}
else if (value < 1000000)
{
str = string.Concat(ConvertDigitAfrikaans(value / 1000), " duisend en");
if (value % 1000 > 0)
{
str = string.Concat(str, " ", ConvertDigitAfrikaans(value % 1000));
}
}
else if (value == 1000000)
{
str = " een miljoen";
}
else if (value < 2000000)
{
str = string.Concat("een miljoen ", ConvertDigitAfrikaans(value % 1000000));
}
else if (value < 1000000000)
{
str = string.Concat(ConvertDigitAfrikaans(value / 1000000), " miljoen");
if (value - value / 1000000 * 1000000 > 0)
{
str = string.Concat(str, " ", ConvertDigitAfrikaans(value - value / 1000000 * 1000000));
}
}
else if (value == 1000000000)
{
str = " een miljard";
}
else if (value < 2000000000)
{
str = string.Concat("een miljard en ", ConvertDigitAfrikaans(value % 1000000000));
}
else if (value < 1000000000000)
{
str = string.Concat(ConvertDigitAfrikaans(value / 1000000000), " miljard");
if (value - value / 1000000000 * 1000000000 > 0)
{
str = string.Concat(str, " ", ConvertDigitAfrikaans(value - value / 1000000000 * 1000000000));
}
}
else if (value == 1000000000000)
{
str = " een biljoen";
}
else if (value < 2000000000000)
{
str = string.Concat("een biljoen en ", ConvertDigitAfrikaans(value % 1000000000000));
}
else if (value < 1000000000000000)
{
str = string.Concat(ConvertDigitAfrikaans(value / 1000000000000), " biljoen");
if (value - value / 1000000000000 * 1000000000000 > 0)
{
str = string.Concat(str, " ", ConvertDigitAfrikaans(value - value / 1000000000000 * 1000000000000));
}
}
return str;
}
/// <summary>
/// Spells numbers in afrikaans language.
/// </summary>
/// <param name="Value"></param>
/// <returns>string</returns>
private static string SpellNumberInAfrikaans(double Value)
{
Int64 num = (Int64)Value;
string str = Value.ToString("0.00#");
char[] chrArray = new char[] { ',' };
string str1 = str.Split(chrArray)[1];
Int64 num1 = Int64.Parse(str1);
if (num1 == 0)
{
return ConvertDigitAfrikaans(num);
}
return string.Format("{0} punt {1} ", ConvertDigitAfrikaans(num), ConvertDigitAfrikaans(num1));
}
#endregion
The above code takes into account the different languages numbers spelling rules. The precision of decimal point digits is 2 digits after the point.
No comments:
Post a Comment