Advertisement
yahorrr

Untitled

Nov 28th, 2022
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  4.  
  5. namespace ConverterDictionaryComposition
  6. {
  7.     /// <summary>
  8.     /// Converts a real number to string.
  9.     /// </summary>
  10.     public class Converter
  11.     {
  12.         private readonly CharsDictionary dictionary;
  13.  
  14.         /// <summary>
  15.         /// Initializes a new instance of the <see cref="Converter"/> class.
  16.         /// </summary>
  17.         /// <param name="dictionaryFactory">Factory of the dictionary with rules of converting.</param>
  18.         /// <exception cref="System.ArgumentNullException">Thrown when dictionary factory is null.</exception>
  19.         public Converter(ICharsDictionaryFactory? dictionaryFactory)
  20.         {
  21.             if (dictionaryFactory == null)
  22.             {
  23.                 throw new ArgumentNullException(nameof(dictionaryFactory));
  24.             }
  25.  
  26.             this.dictionary = dictionaryFactory.CreateDictionary();
  27.         }
  28.  
  29.         /// <summary>
  30.         /// Converts double number into string.
  31.         /// </summary>
  32.         /// <param name="number">Double number to convert.</param>
  33.         /// <returns>A number string representation.</returns>
  34.         public string Convert(double number)
  35.         {
  36.             switch (number)
  37.             {
  38.                 case double.NaN:
  39.                     return this.dictionary.Dictionary[Сharacter.NaN];
  40.                 case double.Epsilon:
  41.                     return this.dictionary.Dictionary[Сharacter.Epsilon];
  42.                 case double.NegativeInfinity:
  43.                     return this.dictionary.Dictionary[Сharacter.NegativeInfinity];
  44.                 case double.PositiveInfinity:
  45.                     return this.dictionary.Dictionary[Сharacter.PositiveInfinity];
  46.             }
  47.  
  48.             string stringNumber = number.ToString(new CultureInfo(this.dictionary.CultureName));
  49.             StringBuilder result = new StringBuilder();
  50.  
  51.             for (int i = 0; i < stringNumber.Length; i++)
  52.             {
  53.                 result.Append(' ').Append(this.dictionary.Dictionary[CharToWord(stringNumber[i])]);
  54.             }
  55.  
  56.             return result.ToString()[1..];
  57.         }
  58.  
  59.         private static Сharacter CharToWord(char digit) =>
  60.             digit switch
  61.             {
  62.                 '-' => Сharacter.Minus,
  63.                 '+' => Сharacter.Plus,
  64.                 '.' => Сharacter.Point,
  65.                 ',' => Сharacter.Comma,
  66.                 'E' => Сharacter.Exponent,
  67.                 '0' => Сharacter.Zero,
  68.                 '1' => Сharacter.One,
  69.                 '2' => Сharacter.Two,
  70.                 '3' => Сharacter.Three,
  71.                 '4' => Сharacter.Four,
  72.                 '5' => Сharacter.Five,
  73.                 '6' => Сharacter.Six,
  74.                 '7' => Сharacter.Seven,
  75.                 '8' => Сharacter.Eight,
  76.                 '9' => Сharacter.Nine
  77.             };
  78.     }
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement