Bu makalede, C# kullanarak sayıları metne ve metinleri sayıya dönüştüren bir sınıf geliştireceğiz.
Bu sınıf, özellikle finansal uygulamalarda, çek yazım sistemlerinde, fatura oluşturma yazılımlarında
ve ses üreten yapay zekalara sayıların metine dönmüş hallerini vermek için kullanılabilir.
using System; using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; public static class NumberTextConverter { // Sayıların yazılı halleri private static readonly string[] Birler = { "", "Bir", "İki", "Üç", "Dört", "Beş", "Altı", "Yedi", "Sekiz", "Dokuz" }; private static readonly string[] Onlar = { "", "On", "Yirmi", "Otuz", "Kırk", "Elli", "Altmış", "Yetmiş", "Seksen", "Doksan" }; private static readonly string[] Binler = { "", "Bin", "Milyon", "Milyar" }; // Sayıyı metne çeviren metod public static string NumberToWords(decimal number) { if (number == 0) return "Sıfır"; int tamKisim = (int)number; // Tam sayı kısmını al int kurusKisim = (int)((number - tamKisim) * 100); // Kuruş kısmını hesapla string tamMetin = ConvertNumberToText(tamKisim); string kurusMetin = kurusKisim > 0 ? ConvertNumberToText(kurusKisim) + " Kuruş" : ""; return (tamMetin + (kurusMetin != "" ? " Lira " + kurusMetin : " Lira")).Trim(); } // 1000'lik basamaklarla sayıyı yazıya döken metod private static string ConvertNumberToText(int number) { if (number == 0) return "Sıfır"; string result = ""; int binIndex = 0; while (number > 0) { int chunk = number % 1000; if (chunk > 0) { string chunkText = ConvertChunk(chunk); result = chunkText + (Binler[binIndex] != "" ? " " + Binler[binIndex] : "") + " " + result; } number /= 1000; binIndex++; } return result.Trim(); } // 100'lük dilimlerde sayıyı metne çeviren metod private static string ConvertChunk(int number) { string result = ""; if (number >= 100) { if (number / 100 == 1) result += "Yüz "; else result += Birler[number / 100] + " Yüz "; number %= 100; } if (number >= 10) { result += Onlar[number / 10] + " "; number %= 10; } if (number > 0) { result += Birler[number] + " "; } return result.Trim(); } // Metni sayıya çeviren metod public static decimal WordsToNumber(string words) { if (string.IsNullOrWhiteSpace(words)) return 0; Dictionary<string, int> wordMap = new Dictionary<string, int> { { "Sıfır", 0 }, { "Bir", 1 }, { "İki", 2 }, { "Üç", 3 }, { "Dört", 4 }, { "Beş", 5 }, { "Altı", 6 }, { "Yedi", 7 }, { "Sekiz", 8 }, { "Dokuz", 9 }, { "On", 10 }, { "Yirmi", 20 }, { "Otuz", 30 }, { "Kırk", 40 }, { "Elli", 50 }, { "Altmış", 60 }, { "Yetmiş", 70 }, { "Seksen", 80 }, { "Doksan", 90 }, { "Yüz", 100 }, { "Bin", 1000 }, { "Milyon", 1000000 }, { "Milyar", 1000000000 } }; decimal result = 0, current = 0; bool isKurus = false; string[] wordsArray = words.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); foreach (string word in wordsArray) { if (word == "Lira") { result += current; current = 0; } else if (word == "Kuruş") { result += current / 100; isKurus = true; current = 0; } else if (wordMap.ContainsKey(word)) { int num = wordMap[word]; if (num == 100) current = (current == 0 ? 1 : current) * num; else if (num >= 1000) { if (current == 0) current = 1; result += current * num; current = 0; } else current += num; } } return result + (isKurus ? 0 : current); } } // Örnek kullanım class Program { static void Main() { Console.WriteLine(NumberTextConverter.NumberToWords(123456.75m)); // "Yüz Yirmi Üç Bin Dört Yüz Elli Altı Lira Yetmiş Beş Kuruş" Console.WriteLine(NumberTextConverter.WordsToNumber("Yüz Yirmi Üç Bin Dört Yüz Elli Altı Lira Yetmiş Beş Kuruş")); // 123456.75 } }