Advertisement
BladeIII

Untitled

Oct 3rd, 2022
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  4.  
  5. #pragma warning disable CA1822
  6.  
  7. namespace TransformToWordsTask
  8. {
  9.     /// <summary>
  10.     /// Provides transformer methods.
  11.     /// </summary>
  12.     public sealed class Transformer
  13.     {
  14.         /// <summary>
  15.         /// Converts number's digital representation into words.
  16.         /// </summary>
  17.         /// <param name="number">Number to convert.</param>
  18.         /// <returns>Words representation.</returns>
  19.         public string TransformToWords(double number)
  20.         {
  21.             switch (number)
  22.             {
  23.                 case double.NegativeInfinity: return "Negative Infinity";
  24.                 case double.PositiveInfinity: return "Positive Infinity";
  25.                 case double.Epsilon: return "Double Epsilon";
  26.                 case double.NaN: return "NaN";
  27.             }
  28.  
  29.             var cultureInfo = new CultureInfo("en-US");
  30.             string stringRepresentation = number.ToString(cultureInfo);
  31.             StringBuilder builder = new StringBuilder();
  32.             for (int i = 0; i < stringRepresentation.Length; i++)
  33.             {
  34.                 builder.Append(GetWordRepresentation(stringRepresentation[i]) + ' ');
  35.             }
  36.  
  37.             builder[0] = char.ToUpper(builder[0], cultureInfo);
  38.             return builder.ToString().TrimEnd();
  39.         }
  40.  
  41.         private static string GetWordRepresentation(char character)
  42.         {
  43.             return character switch
  44.             {
  45.                 '-' => "minus",
  46.                 '1' => "one",
  47.                 '2' => "two",
  48.                 '3' => "three",
  49.                 '4' => "four",
  50.                 '5' => "five",
  51.                 '6' => "six",
  52.                 '7' => "seven",
  53.                 '8' => "eight",
  54.                 '9' => "nine",
  55.                 '0' => "zero",
  56.                 '.' => "point",
  57.                 'E' => "E",
  58.                 '+' => "plus",
  59.                 _ => null
  60.             };
  61.         }
  62.     }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement