Advertisement
MulleDK19

NumbersToText

Sep 5th, 2011
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.10 KB | None | 0 0
  1. // -----------------------------------------------------------------------
  2. // <copyright file="NumbersToText.cs" company="Morten T. Pedersen">
  3. // Copyright © Morten T. Pedersen ® All rights reserved
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6.  
  7. namespace Morten.Helpers
  8. {
  9.     using System;
  10.     using System.Collections.Generic;
  11.     using System.Linq;
  12.     using System.Text;
  13.  
  14.     internal static class NumbersToText
  15.     {
  16.         #region Private Fields
  17.         private static string[] onesMapping = new string[] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
  18.         private static string[] tensMapping = new string[] { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
  19.         private static string[] groupMapping = new string[] { "Hundred", "Thousand", "Million", "Billion", "Trillion" };
  20.         #endregion
  21.  
  22.         #region Methods
  23.         internal static string[] EnglishArrayFromNumber(string number)
  24.         {
  25.             return EnglishArrayFromNumber(Convert.ToInt64(number));
  26.         }
  27.  
  28.         internal static string[] EnglishArrayFromNumber(double number)
  29.         {
  30.             string[] array = EnglishFromNumber(number).Split(' ');
  31.             return array;
  32.         }
  33.  
  34.         internal static string[] EnglishArrayFromNumber(int number)
  35.         {
  36.             return EnglishArrayFromNumber((long)number);
  37.         }
  38.  
  39.         internal static string[] EnglishArrayFromNumber(long number)
  40.         {
  41.             string[] array = EnglishFromNumber(number).Split(' ');
  42.             return array;
  43.         }
  44.  
  45.         internal static string EnglishFromNumber(string number)
  46.         {
  47.             return EnglishFromNumber(Convert.ToInt64(number));
  48.         }
  49.  
  50.         internal static string EnglishFromNumber(int number)
  51.         {
  52.             return EnglishFromNumber((long)number);
  53.         }
  54.  
  55.         internal static string EnglishFromNumber(double number)
  56.         {
  57.             string numberString = number.ToString();
  58.             string[] numberChunks = numberString.Split('.');
  59.             if (numberChunks.Length < 2)
  60.             {
  61.                 return EnglishFromNumber(Convert.ToInt64(number));
  62.             }
  63.  
  64.             string realNumber = EnglishFromNumber(numberChunks[0]);
  65.             List<string> pointDigits = new List<string>();
  66.             for (int i = 0; i < numberChunks[1].Length; ++i)
  67.             {
  68.                 string digit = numberChunks[1].Substring(i, 1);
  69.                 pointDigits.Add(NumbersToText.onesMapping[Convert.ToInt16(digit)]);
  70.             }
  71.  
  72.             string finalNumber = realNumber + " Point";
  73.             foreach (string pointDigit in pointDigits)
  74.             {
  75.                 finalNumber += " " + pointDigit;
  76.             }
  77.  
  78.             return finalNumber;
  79.         }
  80.  
  81.         internal static string EnglishFromNumber(long number)
  82.         {
  83.             if (number == 0)
  84.             {
  85.                 return NumbersToText.onesMapping[number];
  86.             }
  87.  
  88.             string sign = "Positive";
  89.             if (number < 0)
  90.             {
  91.                 sign = "Negative";
  92.                 number = System.Math.Abs(number);
  93.             }
  94.  
  95.             string retVal = null;
  96.             int group = 0;
  97.             while (number > 0)
  98.             {
  99.                 int numberToProcess = (int)(number % 1000);
  100.                 number = number / 1000;
  101.  
  102.                 string groupDescription = ProcessGroup(numberToProcess);
  103.                 if (groupDescription != null)
  104.                 {
  105.                     if (group > 0)
  106.                     {
  107.                         retVal = NumbersToText.groupMapping[group] + " " + retVal;
  108.                     }
  109.  
  110.                     retVal = groupDescription + " " + retVal;
  111.                 }
  112.  
  113.                 ++group;
  114.             }
  115.  
  116.             return sign + " " + retVal.Trim();
  117.         }
  118.  
  119.         private static string ProcessGroup(int number)
  120.         {
  121.             int tens = number % 100;
  122.             int hundreds = number / 100;
  123.  
  124.             string retVal = null;
  125.  
  126.             if (hundreds > 0)
  127.             {
  128.                 retVal = NumbersToText.onesMapping[hundreds] + " " + NumbersToText.groupMapping[0];
  129.             }
  130.  
  131.             if (tens > 0)
  132.             {
  133.                 if (tens < 20)
  134.                 {
  135.                     retVal += ((retVal != null) ? " " : string.Empty) + NumbersToText.onesMapping[tens];
  136.                 }
  137.                 else
  138.                 {
  139.                     int ones = tens % 10;
  140.                     tens = (tens / 10) - 2; // 20's offset
  141.  
  142.                     retVal += ((retVal != null) ? " " : string.Empty) + NumbersToText.tensMapping[tens];
  143.  
  144.                     if (ones > 0)
  145.                     {
  146.                         retVal += ((retVal != null) ? " " : string.Empty) + NumbersToText.onesMapping[ones];
  147.                     }
  148.                 }
  149.             }
  150.  
  151.             return retVal;
  152.         }
  153.         #endregion
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement