Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Globalization;
- using System.Text;
- namespace ConverterDictionaryComposition
- {
- /// <summary>
- /// Converts a real number to string.
- /// </summary>
- public class Converter
- {
- private readonly CharsDictionary dictionary;
- /// <summary>
- /// Initializes a new instance of the <see cref="Converter"/> class.
- /// </summary>
- /// <param name="dictionaryFactory">Factory of the dictionary with rules of converting.</param>
- /// <exception cref="System.ArgumentNullException">Thrown when dictionary factory is null.</exception>
- public Converter(ICharsDictionaryFactory? dictionaryFactory)
- {
- if (dictionaryFactory == null)
- {
- throw new ArgumentNullException(nameof(dictionaryFactory));
- }
- this.dictionary = dictionaryFactory.CreateDictionary();
- }
- /// <summary>
- /// Converts double number into string.
- /// </summary>
- /// <param name="number">Double number to convert.</param>
- /// <returns>A number string representation.</returns>
- public string Convert(double number)
- {
- switch (number)
- {
- case double.NaN:
- return this.dictionary.Dictionary[Сharacter.NaN];
- case double.Epsilon:
- return this.dictionary.Dictionary[Сharacter.Epsilon];
- case double.NegativeInfinity:
- return this.dictionary.Dictionary[Сharacter.NegativeInfinity];
- case double.PositiveInfinity:
- return this.dictionary.Dictionary[Сharacter.PositiveInfinity];
- }
- string stringNumber = number.ToString(new CultureInfo(this.dictionary.CultureName));
- StringBuilder result = new StringBuilder();
- for (int i = 0; i < stringNumber.Length; i++)
- {
- result.Append(' ').Append(this.dictionary.Dictionary[CharToWord(stringNumber[i])]);
- }
- return result.ToString()[1..];
- }
- private static Сharacter CharToWord(char digit) =>
- digit switch
- {
- '-' => Сharacter.Minus,
- '+' => Сharacter.Plus,
- '.' => Сharacter.Point,
- ',' => Сharacter.Comma,
- 'E' => Сharacter.Exponent,
- '0' => Сharacter.Zero,
- '1' => Сharacter.One,
- '2' => Сharacter.Two,
- '3' => Сharacter.Three,
- '4' => Сharacter.Four,
- '5' => Сharacter.Five,
- '6' => Сharacter.Six,
- '7' => Сharacter.Seven,
- '8' => Сharacter.Eight,
- '9' => Сharacter.Nine
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement