Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // -----------------------------------------------------------------------
- // <copyright file="NumbersToText.cs" company="Morten T. Pedersen">
- // Copyright © Morten T. Pedersen ® All rights reserved
- // </copyright>
- // -----------------------------------------------------------------------
- namespace Morten.Helpers
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- internal static class NumbersToText
- {
- #region Private Fields
- 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" };
- private static string[] tensMapping = new string[] { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
- private static string[] groupMapping = new string[] { "Hundred", "Thousand", "Million", "Billion", "Trillion" };
- #endregion
- #region Methods
- internal static string[] EnglishArrayFromNumber(string number)
- {
- return EnglishArrayFromNumber(Convert.ToInt64(number));
- }
- internal static string[] EnglishArrayFromNumber(double number)
- {
- string[] array = EnglishFromNumber(number).Split(' ');
- return array;
- }
- internal static string[] EnglishArrayFromNumber(int number)
- {
- return EnglishArrayFromNumber((long)number);
- }
- internal static string[] EnglishArrayFromNumber(long number)
- {
- string[] array = EnglishFromNumber(number).Split(' ');
- return array;
- }
- internal static string EnglishFromNumber(string number)
- {
- return EnglishFromNumber(Convert.ToInt64(number));
- }
- internal static string EnglishFromNumber(int number)
- {
- return EnglishFromNumber((long)number);
- }
- internal static string EnglishFromNumber(double number)
- {
- string numberString = number.ToString();
- string[] numberChunks = numberString.Split('.');
- if (numberChunks.Length < 2)
- {
- return EnglishFromNumber(Convert.ToInt64(number));
- }
- string realNumber = EnglishFromNumber(numberChunks[0]);
- List<string> pointDigits = new List<string>();
- for (int i = 0; i < numberChunks[1].Length; ++i)
- {
- string digit = numberChunks[1].Substring(i, 1);
- pointDigits.Add(NumbersToText.onesMapping[Convert.ToInt16(digit)]);
- }
- string finalNumber = realNumber + " Point";
- foreach (string pointDigit in pointDigits)
- {
- finalNumber += " " + pointDigit;
- }
- return finalNumber;
- }
- internal static string EnglishFromNumber(long number)
- {
- if (number == 0)
- {
- return NumbersToText.onesMapping[number];
- }
- string sign = "Positive";
- if (number < 0)
- {
- sign = "Negative";
- number = System.Math.Abs(number);
- }
- string retVal = null;
- int group = 0;
- while (number > 0)
- {
- int numberToProcess = (int)(number % 1000);
- number = number / 1000;
- string groupDescription = ProcessGroup(numberToProcess);
- if (groupDescription != null)
- {
- if (group > 0)
- {
- retVal = NumbersToText.groupMapping[group] + " " + retVal;
- }
- retVal = groupDescription + " " + retVal;
- }
- ++group;
- }
- return sign + " " + retVal.Trim();
- }
- private static string ProcessGroup(int number)
- {
- int tens = number % 100;
- int hundreds = number / 100;
- string retVal = null;
- if (hundreds > 0)
- {
- retVal = NumbersToText.onesMapping[hundreds] + " " + NumbersToText.groupMapping[0];
- }
- if (tens > 0)
- {
- if (tens < 20)
- {
- retVal += ((retVal != null) ? " " : string.Empty) + NumbersToText.onesMapping[tens];
- }
- else
- {
- int ones = tens % 10;
- tens = (tens / 10) - 2; // 20's offset
- retVal += ((retVal != null) ? " " : string.Empty) + NumbersToText.tensMapping[tens];
- if (ones > 0)
- {
- retVal += ((retVal != null) ? " " : string.Empty) + NumbersToText.onesMapping[ones];
- }
- }
- }
- return retVal;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement