1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class ToWord
{
/// Group Levels: 987,654,321.234
/// 234 : Group Level -1
/// 321 : Group Level 0
/// 654 : Group Level 1
/// 987 : Group Level 2
#region Varaibles & Properties
/// <summary>
/// integer part
/// </summary>
private long _intergerValue;
/// <summary>
/// Decimal Part
/// </summary>
private int _decimalValue;
/// <summary>
/// Number to be converted
/// </summary>
public Decimal Number { get; set; }
/// <summary>
/// Currency to use
/// </summary>
public CurrencyInfo Currency { get; set; }
/// <summary>
/// Frensh text to be placed before the generated text
/// </summary>
public String FrenshPrefixText { get; set; }
/// <summary>
/// Frensh text to be placed after the generated text
/// </summary>
public String FrenshSuffixText { get; set; }
/// <summary>
/// Arabic text to be placed before the generated text
/// </summary>
public String ArabicPrefixText { get; set; }
/// <summary>
/// Arabic text to be placed after the generated text
/// </summary>
public String ArabicSuffixText { get; set; }
#endregion
#region General
/// <summary>
/// Constructor: short version
/// </summary>
/// <param name="number">Number to be converted</param>
/// <param name="currency">Currency to use</param>
public ToWord(Decimal number, CurrencyInfo currency)
{
InitializeClass(number, currency, String.Empty, "only.", "", "");
}
/// <summary>
/// Constructor: Full Version
/// </summary>
/// <param name="number">Number to be converted</param>
/// <param name="currency">Currency to use</param>
/// <param name="FrenshPrefixText">Frensh text to be placed before the generated text</param>
/// <param name="FrenshSuffixText">Frensh text to be placed after the generated text</param>
/// <param name="arabicPrefixText">Arabic text to be placed before the generated text</param>
/// <param name="arabicSuffixText">Arabic text to be placed after the generated text</param>
public ToWord(Decimal number, CurrencyInfo currency, String FrenshPrefixText, String FrenshSuffixText, String arabicPrefixText, String arabicSuffixText)
{
InitializeClass(number, currency, FrenshPrefixText, FrenshSuffixText, arabicPrefixText, arabicSuffixText);
}
/// <summary>
/// Initialize Class Varaibles
/// </summary>
/// <param name="number">Number to be converted</param>
/// <param name="currency">Currency to use</param>
/// <param name="FrenshPrefixText">Frensh text to be placed before the generated text</param>
/// <param name="FrenshSuffixText">Frensh text to be placed after the generated text</param>
/// <param name="arabicPrefixText">Arabic text to be placed before the generated text</param>
/// <param name="arabicSuffixText">Arabic text to be placed after the generated text</param>
private void InitializeClass(Decimal number, CurrencyInfo currency, String FrenshPrefixText, String FrenshSuffixText, String arabicPrefixText, String arabicSuffixText)
{
Number = number;
Currency = currency;
FrenshPrefixText = FrenshPrefixText;
FrenshSuffixText = FrenshSuffixText;
ArabicPrefixText = arabicPrefixText;
ArabicSuffixText = arabicSuffixText;
ExtractIntegerAndDecimalParts();
}
/// <summary>
/// Get Proper Decimal Value
/// </summary>
/// <param name="decimalPart">Decimal Part as a String</param>
/// <returns></returns>
private string GetDecimalValue(string decimalPart)
{
string result = String.Empty;
if (Currency.PartPrecision != decimalPart.Length)
{
int decimalPartLength = decimalPart.Length;
for (int i = 0; i < Currency.PartPrecision - decimalPartLength; i++)
{
decimalPart += "0"; //Fix for 1 number after decimal ( 10.5 , 1442.2 , 375.4 )
}
result = String.Format("{0}.{1}", decimalPart.Substring(0, Currency.PartPrecision), decimalPart.Substring(Currency.PartPrecision, decimalPart.Length - Currency.PartPrecision));
result = (Math.Round(Convert.ToDecimal(result))).ToString();
}
else
result = decimalPart;
for (int i = 0; i < Currency.PartPrecision - result.Length; i++)
{
result += "0";
}
return result;
}
/// <summary>
/// Eextract Interger and Decimal parts
/// </summary>
private void ExtractIntegerAndDecimalParts()
{
String[] splits = Number.ToString().Split('.');
_intergerValue = Convert.ToInt32(splits[0]);
if (splits.Length > 1)
_decimalValue = Convert.ToInt32(GetDecimalValue(splits[1]));
}
#endregion
#region Frensh Number To Word
#region Varaibles
private static string[] FrenshOnes =
new string[] {
"Zéro", "Un", "Deux", "Trois", "Quatre", "Cinq", "Six", "Sept", "Huit", "Neuf",
"Dix", "Onze", "Douze", "Treize", "Quatorze", "Quinze", "Seize", "Dix-sept", "Dix-huit", "Dix-neuf"
};
private static string[] FrenshTens =
new string[] {
"Vingt", "Trente", "Quarante", "Cinquante", "Soixante", "Soixante-dix", "Quatre-vingts", "Quatre-vingt-dix"
};
private static string[] FrenshGroup =
new string[] {
"Cent", "Mille", "Million", "Milliard", "Trillion", "Quadrillion", "Quintillion", "Sextillian",
"Septillion", "Octillion", "Nonillion", "Decillion", "Undecillion", "Duodecillion", "Tredecillion",
"Quattuordecillion", "Quindecillion", "Sexdecillion", "Septendecillion", "Octodecillion", "Novemdecillion",
"Vigintillion", "Unvigintillion", "Duovigintillion", "10^72", "10^75", "10^78", "10^81", "10^84", "10^87",
"Vigintinonillion", "10^93", "10^96", "Duotrigintillion", "Trestrigintillion"
};
#endregion
/// <summary>
/// Process a group of 3 digits
/// </summary>
/// <param name="groupNumber">The group number to process</param>
/// <returns></returns>
private string ProcessGroup(int groupNumber)
{
int tens = groupNumber % 100;
int hundreds = groupNumber / 100;
string retVal = String.Empty;
if (hundreds > 0)
{
retVal = String.Format("{0} {1}", FrenshOnes[hundreds], FrenshGroup[0]);
}
if (tens > 0)
{
if (tens < 20)
{
retVal += ((retVal != String.Empty) ? " " : String.Empty) + FrenshOnes[tens];
}
else
{
int ones = tens % 10;
tens = (tens / 10) - 2; // 20's offset
retVal += ((retVal != String.Empty) ? " " : String.Empty) + FrenshTens[tens];
if (ones > 0)
{
retVal += ((retVal != String.Empty) ? " " : String.Empty) + FrenshOnes[ones];
}
}
}
return retVal;
}
/// <summary>
/// Convert stored number to words using selected currency
/// </summary>
/// <returns></returns>
public string ConvertToFrensh()
{
Decimal tempNumber = Number;
if (tempNumber == 0)
return "Zero";
string decimalString = ProcessGroup(_decimalValue);
string retVal = String.Empty;
int group = 0;
if (tempNumber < 1)
{
retVal = FrenshOnes[0];
}
else
{
while (tempNumber >= 1)
{
int numberToProcess = (int)(tempNumber % 1000);
tempNumber = tempNumber / 1000;
string groupDescription = ProcessGroup(numberToProcess);
if (groupDescription != String.Empty)
{
if (group > 0)
{
retVal = String.Format("{0} {1}", FrenshGroup[group], retVal);
}
retVal = String.Format("{0} {1}", groupDescription, retVal);
}
group++;
}
}
String formattedNumber = String.Empty;
formattedNumber += (FrenshPrefixText != String.Empty) ? String.Format("{0} ", FrenshPrefixText) : String.Empty;
formattedNumber += (retVal != String.Empty) ? retVal : String.Empty;
formattedNumber += (retVal != String.Empty) ? (_intergerValue == 1 ? Currency.FrenshCurrencyName : Currency.FrenshPluralCurrencyName) : String.Empty;
formattedNumber += (decimalString != String.Empty) ? " et " : String.Empty;
formattedNumber += (decimalString != String.Empty) ? decimalString : String.Empty;
formattedNumber += (decimalString != String.Empty) ? " " + (_decimalValue == 1 ? Currency.FrenshCurrencyPartName : Currency.FrenshPluralCurrencyPartName) : String.Empty;
formattedNumber += (FrenshSuffixText != String.Empty) ? String.Format(" {0}", FrenshSuffixText) : String.Empty;
return formattedNumber;
}
#endregion
#region Arabic Number To Word
#region Varaibles
private static string[] arabicOnes =
new string[] {
String.Empty, "واحد", "اثنان", "ثلاثة", "أربعة", "خمسة", "ستة", "سبعة", "ثمانية", "تسعة",
"عشرة", "أحد عشر", "اثنا عشر", "ثلاثة عشر", "أربعة عشر", "خمسة عشر", "ستة عشر", "سبعة عشر", "ثمانية عشر", "تسعة عشر"
};
private static string[] arabicFeminineOnes =
new string[] {
String.Empty, "إحدى", "اثنتان", "ثلاث", "أربع", "خمس", "ست", "سبع", "ثمان", "تسع",
"عشر", "إحدى عشرة", "اثنتا عشرة", "ثلاث عشرة", "أربع عشرة", "خمس عشرة", "ست عشرة", "سبع عشرة", "ثماني عشرة", "تسع عشرة"
};
private static string[] arabicTens =
new string[] {
"عشرون", "ثلاثون", "أربعون", "خمسون", "ستون", "سبعون", "ثمانون", "تسعون"
};
private static string[] arabicHundreds =
new string[] {
"", "مائة", "مئتان", "ثلاثمائة", "أربعمائة", "خمسمائة", "ستمائة", "سبعمائة", "ثمانمائة","تسعمائة"
};
private static string[] arabicAppendedTwos =
new string[] {
"مئتا", "ألف", "مليونا", "مليارا", "تريليونا", "كوادريليونا", "كوينتليونا", "سكستيليونا"
};
private static string[] arabicTwos =
new string[] {
"مئتان", "ألفان", "مليونان", "ملياران", "تريليونان", "كوادريليونان", "كوينتليونان", "سكستيليونان"
};
private static string[] arabicGroup =
new string[] {
"مائة", "ألف", "مليون", "مليار", "تريليون", "كوادريليون", "كوينتليون", "سكستيليون"
};
private static string[] arabicAppendedGroup =
new string[] {
"", "ألف", "مليون", "مليار", "تريليون", "كوادريليون", "كوينتليون", "سكستيليون"
};
private static string[] arabicPluralGroups =
new string[] {
"", "آلاف", "ملايين", "مليارات", "تريليونات", "كوادريليونات", "كوينتليونات", "سكستيليونات"
};
#endregion
/// <summary>
/// Get Feminine Status of one digit
/// </summary>
/// <param name="digit">The Digit to check its Feminine status</param>
/// <param name="groupLevel">Group Level</param>
/// <returns></returns>
private string GetDigitFeminineStatus(int digit, int groupLevel)
{
if (groupLevel == -1)
{ // if it is in the decimal part
if (Currency.IsCurrencyPartNameFeminine)
return arabicFeminineOnes[digit]; // use feminine field
else
return arabicOnes[digit];
}
else
if (groupLevel == 0)
{
if (Currency.IsCurrencyNameFeminine)
return arabicFeminineOnes[digit];// use feminine field
else
return arabicOnes[digit];
}
else
return arabicOnes[digit];
}
/// <summary>
/// Process a group of 3 digits
/// </summary>
/// <param name="groupNumber">The group number to process</param>
/// <returns></returns>
private string ProcessArabicGroup(int groupNumber, int groupLevel, Decimal remainingNumber)
{
int tens = groupNumber % 100;
int hundreds = groupNumber / 100;
string retVal = String.Empty;
if (hundreds > 0)
{
if (tens == 0 && hundreds == 2) // حالة المضاف
retVal = String.Format("{0}", arabicAppendedTwos[0]);
else // الحالة العادية
retVal = String.Format("{0}", arabicHundreds[hundreds]);
}
if (tens > 0)
{
if (tens < 20)
{ // if we are processing under 20 numbers
if (tens == 2 && hundreds == 0 && groupLevel > 0)
{ // This is special case for number 2 when it comes alone in the group
if (_intergerValue == 2000 || _intergerValue == 2000000 || _intergerValue == 2000000000 || _intergerValue == 2000000000000 || _intergerValue == 2000000000000000 || _intergerValue == 2000000000000000000)
retVal = String.Format("{0}", arabicAppendedTwos[groupLevel]); // في حالة الاضافة
else
retVal = String.Format("{0}", arabicTwos[groupLevel]);// في حالة الافراد
}
else
{ // General case
if (retVal != String.Empty)
retVal += " و ";
if (tens == 1 && groupLevel > 0 && hundreds == 0)
retVal += " ";
else
if ((tens == 1 || tens == 2) && (groupLevel == 0 || groupLevel == -1) && hundreds == 0 && remainingNumber == 0)
retVal += String.Empty; // Special case for 1 and 2 numbers like: ليرة سورية و ليرتان سوريتان
else
retVal += GetDigitFeminineStatus(tens, groupLevel);// Get Feminine status for this digit
}
}
else
{
int ones = tens % 10;
tens = (tens / 10) - 2; // 20's offset
if (ones > 0)
{
if (retVal != String.Empty)
retVal += " و ";
// Get Feminine status for this digit
retVal += GetDigitFeminineStatus(ones, groupLevel);
}
if (retVal != String.Empty)
retVal += " و ";
// Get Tens text
retVal += arabicTens[tens];
}
}
return retVal;
}
/// <summary>
/// Convert stored number to words using selected currency
/// </summary>
/// <returns></returns>
public string ConvertToArabic()
{
Decimal tempNumber = Number;
if (tempNumber == 0)
return "صفر";
// Get Text for the decimal part
string decimalString = ProcessArabicGroup(_decimalValue, -1, 0);
string retVal = String.Empty;
Byte group = 0;
while (tempNumber >= 1)
{
// seperate number into groups
int numberToProcess = (int)(tempNumber % 1000);
tempNumber = tempNumber / 1000;
// convert group into its text
string groupDescription = ProcessArabicGroup(numberToProcess, group, Math.Floor(tempNumber));
if (groupDescription != String.Empty)
{ // here we add the new converted group to the previous concatenated text
if (group > 0)
{
if (retVal != String.Empty)
retVal = String.Format("{0} {1}", "و", retVal);
if (numberToProcess != 2)
{
if (numberToProcess % 100 != 1)
{
if (numberToProcess >= 3 && numberToProcess <= 10) // for numbers between 3 and 9 we use plural name
retVal = String.Format("{0} {1}", arabicPluralGroups[group], retVal);
else
{
if (retVal != String.Empty) // use appending case
retVal = String.Format("{0} {1}", arabicAppendedGroup[group], retVal);
else
retVal = String.Format("{0} {1}", arabicGroup[group], retVal); // use normal case
}
}
else
{
retVal = String.Format("{0} {1}", arabicGroup[group], retVal); // use normal case
}
}
}
retVal = String.Format("{0} {1}", groupDescription, retVal);
}
group++;
}
String formattedNumber = String.Empty;
formattedNumber += (ArabicPrefixText != String.Empty) ? String.Format("{0} ", ArabicPrefixText) : String.Empty;
formattedNumber += (retVal != String.Empty) ? retVal : String.Empty;
if (_intergerValue != 0)
{ // here we add currency name depending on _intergerValue : 1 ,2 , 3--->10 , 11--->99
int remaining100 = (int)(_intergerValue % 100);
if (remaining100 == 0)
formattedNumber += Currency.Arabic1CurrencyName;
else
if (remaining100 == 1)
formattedNumber += Currency.Arabic1CurrencyName;
else
if (remaining100 == 2)
{
if (_intergerValue == 2)
formattedNumber += Currency.Arabic2CurrencyName;
else
formattedNumber += Currency.Arabic1CurrencyName;
}
else
if (remaining100 >= 3 && remaining100 <= 10)
formattedNumber += Currency.Arabic310CurrencyName;
else
if (remaining100 >= 11 && remaining100 <= 99)
formattedNumber += Currency.Arabic1199CurrencyName;
}
formattedNumber += (_decimalValue != 0) ? " و " : String.Empty;
formattedNumber += (_decimalValue != 0) ? decimalString : String.Empty;
if (_decimalValue != 0)
{ // here we add currency part name depending on _intergerValue : 1 ,2 , 3--->10 , 11--->99
formattedNumber += " ";
int remaining100 = (int)(_decimalValue % 100);
if (remaining100 == 0)
formattedNumber += Currency.Arabic1CurrencyPartName;
else
if (remaining100 == 1)
formattedNumber += Currency.Arabic1CurrencyPartName;
else
if (remaining100 == 2)
formattedNumber += Currency.Arabic2CurrencyPartName;
else
if (remaining100 >= 3 && remaining100 <= 10)
formattedNumber += Currency.Arabic310CurrencyPartName;
else
if (remaining100 >= 11 && remaining100 <= 99)
formattedNumber += Currency.Arabic1199CurrencyPartName;
}
formattedNumber += (ArabicSuffixText != String.Empty) ? String.Format(" {0}", ArabicSuffixText) : String.Empty;
return formattedNumber;
}
#endregion
} |
Partager