Advertisement
yahorrr

Untitled

Sep 23rd, 2022
984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Globalization;
  4. using System.Reflection.Metadata.Ecma335;
  5. using System.Text;
  6. using System.Xml;
  7.  
  8. namespace TransformerToWords
  9. {
  10.     /// <summary>
  11.     /// Implements transformer class.
  12.     /// </summary>
  13.     public class Transformer
  14.     {
  15.         /// <summary>
  16.         /// Transforms each element of source array into its 'word format'.
  17.         /// </summary>
  18.         /// <param name="source">Source array.</param>
  19.         /// <returns>Array of 'word format' of elements of source array.</returns>
  20.         /// <exception cref="ArgumentNullException">Thrown when array is null.</exception>
  21.         /// <exception cref="ArgumentException">Thrown when array is empty.</exception>
  22.         /// <example>
  23.         /// new[] { 2.345, -0.0d, 0.0d, 0.1d } => { "Two point three four five", "Minus zero", "Zero", "Zero point one" }.
  24.         /// </example>
  25.         public string[] Transform(double[]? source)
  26.         {
  27.             if (source == null)
  28.             {
  29.                 throw new ArgumentNullException(nameof(source), "array is null");
  30.             }
  31.  
  32.             if (source.Length == 0)
  33.             {
  34.                 throw new ArgumentException("array length is zero", nameof(source));
  35.             }
  36.  
  37.             string[] result = new string[source.Length];
  38.  
  39.             for (int i = 0; i < source.Length; i++)
  40.             {
  41.                 result[i] = NumberToWord(source[i]);
  42.             }
  43.  
  44.             return result;
  45.         }
  46.  
  47.         private static string NumberToWord(double number)
  48.         {
  49.             switch (number)
  50.             {
  51.                 case double.NaN:
  52.                     return "Not a Number";
  53.                 case double.Epsilon:
  54.                     return "Double Epsilon";
  55.                 case double.NegativeInfinity:
  56.                     return "Negative Infinity";
  57.                 case double.PositiveInfinity:
  58.                     return "Positive Infinity";
  59.             }
  60.  
  61.             string stringNumber = number.ToString(CultureInfo.InvariantCulture);
  62.             StringBuilder result = new StringBuilder();
  63.  
  64.             for (int i = 0; i < stringNumber.Length; i++)
  65.             {
  66.                 result.Append(CharToWord(stringNumber[i]));
  67.             }
  68.  
  69.             return char.ToUpper(result[1], CultureInfo.InvariantCulture) + result.ToString()[2..];
  70.         }
  71.  
  72.         private static string? CharToWord(char digit) =>
  73.             digit switch
  74.             {
  75.                 '-' => " minus",
  76.                 '+' => " plus",
  77.                 '.' => " point",
  78.                 'E' => " E",
  79.                 '0' => " zero",
  80.                 '1' => " one",
  81.                 '2' => " two",
  82.                 '3' => " three",
  83.                 '4' => " four",
  84.                 '5' => " five",
  85.                 '6' => " six",
  86.                 '7' => " seven",
  87.                 '8' => " eight",
  88.                 '9' => " nine",
  89.                 _ => null
  90.             };
  91.     }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement